Friday, April 25, 2014

NullPointerException and ArrayIndexOutOfBoundsException...


After refactoring my project(thanks to this How to associate two classes? ) I encountered some problems with one table.


While I use table without specific length I get:


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0

And if I change it to table with lenght 1500 I get:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

How I create table:

public String players[] = new String[1500]; // null pointer exception

//public String players[] = {}; // array index out of bounds exception



When everything was in one class it was working fine. But now... I have no idea how to handle it.

If its important table can get from 0 to around 1300~~ So there always we have some nulls while using [1500] table.

So, here is the code:

Init:



Java Code:



package com._2nozz;

import java.awt.EventQueue;

import javax.swing.JFrame;

public class Init extends JFrame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Panel();
}
});
}
}

Panel:


Java Code:



package com._2nozz;

import javax.swing.JFrame;

public class Panel extends JFrame {

static int windowWidth = 818;
static int windowHeight = 495;

public Panel() {
super("Mass");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setLocation(400, 200);
setSize(windowWidth, windowHeight);
add(new Program());
setVisible(true);
}
}

Program:


Java Code:



package com._2nozz;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Program extends JPanel implements ActionListener {

boolean gotList = false;

GuiParts gui = new GuiParts(this);
Players p = new Players(null);

public Program() {
setLayout(null);
gui.addButtons();
gui.addTextAreas();
}

@Override
public void actionPerformed(ActionEvent e) {
Object click = e.getSource();

if (click == gui.getPlayers) {
p.getPlayersList();
p.printPlayers();
} else if (click == gui.send) {
p.startSending();
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

if (gotList == true)
g.drawString("Players Online: " + p.nickCounter, 200, 250);

g.drawString(
"Some informations about program, author, shit and things."
+ " Also some logs, ads etc.",
30, 300);
g.drawString("This button do nothing yet. > ", 250, 220);

}

}

Players:


Java Code:



package com._2nozz;

import java.io.*;
import java.net.*;

import javax.swing.*;

public class Players {
private Program program;

public Players(Program program) {
this.program = program;
}

GuiParts gui = new GuiParts(program);

public String players[] = new String[1500]; // null pointer exception
//public String players[] = {}; // array index out of bounds exception

int trashLength = 9;
int length = 0;
int nickCounter = 0;

String line = null;
String link = "<a href=\"http://ift.tt/1nNWKT7";

public void getPlayersList() {
try {
createPlayersTable();
} catch (IOException e1) {
e1.printStackTrace();
}
}

public void createPlayersTable() throws IOException {
URL url = new URL(link.substring(trashLength, link.length()));
URLConnection con;
con = url.openConnection();
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null) {
if (line.contains("/players/show/")) {

for (int i = link.length() + trashLength; i < line.length()
+ trashLength; i++) {
if (line.charAt(i) == '\"')
break;
length++;
}
if (line.substring(47, 49 + length).contains("%5") == false)
players[nickCounter] = line.substring(47, 49 + length);
nickCounter++;
length = 0;
}
if (line.contains("class=\"Top10\""))
break;
}
cleanListHash();
saveListState();
}

public void cleanListHash() {
// convert _ to space, % things to special chars in every players[]
// entry
}

public void saveListState() {
program.gotList = true;
gui.send.setEnabled(true);
program.repaint();
}

public void printPlayers() {
for (int i = 0; i <= nickCounter; i++)
if (players[i] != null)
gui.playersList.append(players[i] + "\n");
}

public void startSending() {
// start sending messages to every player on list
program.repaint();
}

}

GuiParts:


Java Code:



package com._2nozz;

import javax.swing.*;

public class GuiParts {

int smallButtonH = 25, largeButtonH = 50;
int gap = 15, smallGap = gap / 2;

int playerListW = Panel.windowWidth / 4, playerListH = Panel.windowHeight
- smallButtonH - (gap * 5);
int playerListX = Panel.windowWidth - playerListW - (2 * gap),
playerListY = gap;

int logW = Panel.windowWidth - (4 * gap) - playerListW,
logH = Panel.windowHeight / 3;
int logX = gap, logY = gap;

int listPanelButtonW = 120;
int listPanelButtonX = playerListX, listPanelButtonY = playerListH
+ (gap + smallGap);

int floodButtonW = 150;
int floodButtonX = Panel.windowWidth - playerListW - (3 * gap)
- floodButtonW, floodButtonY = logH + (gap + smallGap);

private Program program;

public GuiParts(Program program) {
this.program = program;
}

JButton getPlayers;
JButton send;
JTextArea playersList, messagesLog;

public void addButtons() {
getPlayers = new JButton("Get Player List");
getPlayers.addActionListener(program);
getPlayers.setBounds(listPanelButtonX, listPanelButtonY,
listPanelButtonW, smallButtonH);
program.add(getPlayers);

send = new JButton("START");
send.addActionListener(program);
send.setBounds(floodButtonX, floodButtonY, floodButtonW, largeButtonH);
send.setEnabled(false);
program.add(send);
}

public void addTextAreas() {
playersList = new JTextArea();
JScrollPane playersScroller = new JScrollPane(playersList);
playersScroller.setBounds(playerListX, playerListY, playerListW,
playerListH);
playersList.setEditable(true);
program.add(playersScroller);

messagesLog = new JTextArea();
JScrollPane logScroller = new JScrollPane(messagesLog);
logScroller.setBounds(logX, logY, logW, logH);
messagesLog.setLineWrap(true);
messagesLog.setWrapStyleWord(true);
messagesLog.setEditable(false);
messagesLog
.append("Some kind of welcome message with informations about program"
+ " and explaination how it works."
+ " Maybe tutorial link or something...");
program.add(logScroller);
}

}

Sorry, I know, no comments in code :/ I will write them later...

And two more things.

Can you say something about this project build? Some advices? I'm currently working on it, so there will be few changes anyway. But it would be nice to get some advice.

And second. How to easy upload bigger project to forums? Its little problematically to copy every single file. And if someone want to help and try to compile it, he has to copy it also... Can I somehow export it? Well, I think I can, but I don't know how to use export in eclipse to do that :/


Btw. Should I post this here in New to Java, or any other subforum? If I would choose, with my faith in my abilities, it will always be new to java ;P



No comments:

Post a Comment