Results 1 to 17 of 17

Thread: NullPointerException and ArrayIndexOutOfBoundsException...
- 04-25-2014, 10:09 PM #1
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
NullPointerException and ArrayIndexOutOfBoundsException...
After refactoring my project(thanks to this http://www.java-forums.org/new-java/...o-classes.html ) 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
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
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(); } }); } }
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); } }
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); } }
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://vestia.pl/players/online"; 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(); } }
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); } }
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
- 04-25-2014, 10:37 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 10
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Try to print nickCounter after you change it to see what values it reaches and where it fires exception..
- 04-25-2014, 10:45 PM #3
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Exception fires while there is not enough things to put in table. Then table gets nulls. It was working while it was in one class, and now isn't, thats what I can't understand...
And while I use second method without specific table length, it always give me 0.
Well, I have an idea that I will try. Make only counter in first loop. Then create table with specific length. And then there won't be any nulls. Maybe it will works?
Will edit post in few mins.
@edit
Damn, while I create table in createPlayersTable its not global and I can't use it outside... So I have some errors with that.Last edited by Nozz; 04-25-2014 at 10:49 PM.
- 04-25-2014, 10:54 PM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 10
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Ok, what method gives you exceptions, createPlayersTable() or printPlayers() or something else (I didn't look thoroughly)?
- 04-25-2014, 10:55 PM #5
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
Re: NullPointerException and ArrayIndexOutOfBoundsException...
...
Counter: 515, Length: 1500
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com._2nozz.Players.saveListState(Players.java:77)
at com._2nozz.Players.createPlayersTable(Players.java :68)
at com._2nozz.Players.getPlayersList(Players.java:30)
at com._2nozz.Program.actionPerformed(Program.java:26 )
...
Btw. Tried to fill table to the end with something, but it doesn't help...
for(int i = 1; i < players.length-nickCounter; i++)
players[nickCounter+i] = "something";Last edited by Nozz; 04-25-2014 at 10:58 PM.
- 04-25-2014, 11:00 PM #6
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com._2nozz.Players.saveListState(Players.java:77)If you don't understand my response, don't ignore it, ask a question.
- 04-25-2014, 11:05 PM #7
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
Re: NullPointerException and ArrayIndexOutOfBoundsException...
gotList
But... Its not null. Its never null...
Or is it?
@edit
Ok, moved gotList to Players, little fix in Program and its not problem anymore.
But still. I did something wrong with associating these classes.
Now:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com._2nozz.Players.saveListState(Players.java:81)
at com._2nozz.Players.createPlayersTable(Players.java :71)
at com._2nozz.Players.getPlayersList(Players.java:31)
at com._2nozz.Program.actionPerformed(Program.java:24 )
gui.send.setEnabled(true);
Is this good?
http://scr.hu/0xu3/6hbxzLast edited by Nozz; 04-25-2014 at 11:11 PM.
- 04-25-2014, 11:13 PM #8
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Add a printn() statement just before line 77 that prints out the values of all the variables used on line 77 so you can see which one has the null value.
For example if the statement was: ad.dc.get().doIt() you need to print the values of ad, ad.dc and ad.dc.get()If you don't understand my response, don't ignore it, ask a question.
- 04-25-2014, 11:14 PM #9
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 311
- Rep Power
- 10
Re: NullPointerException and ArrayIndexOutOfBoundsException...
please put your method saveListState() here to see how it looks now
- 04-25-2014, 11:18 PM #10
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Java Code:System.out.println("gui: "+gui+" | gui.send: "+gui.send+" | gotlist: "+gotList+"");
Java Code:gui: com._2nozz.GuiParts@2aeb09a5 | gui.send: null | gotlist: false
Java Code:public void saveListState() { System.out.println("gui: "+gui+" | gui.send: "+gui.send+" | gotlist: "+gotList+""); gui.send.setEnabled(true); gotList = true; program.repaint(); }
if (p.gotList == true)
Problem is now on gui.send... Why?Last edited by Nozz; 04-25-2014 at 11:21 PM.
- 04-25-2014, 11:27 PM #11
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Problem is now on gui.send... Why?
Is there more than one instances of that object? Add a println() to the constructor of the class to see how many are created.If you don't understand my response, don't ignore it, ask a question.
- 04-25-2014, 11:39 PM #12
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
Re: NullPointerException and ArrayIndexOutOfBoundsException...
I commented this to see if it can run without this, and its not the only problem.
public void saveListState() {
System.out.println("program: "+program+" | gui.send: "+gui.send);
//gui.send.setEnabled(true);
gotList = true;
program.repaint();
}
returns:
program: null | gui.send: null
Where is send given a value? Is that code executed for the instance of the object that the variable: gui refers to.
Is there more than one instances of that object? Add a println() to the constructor of the class to see how many are created.
I don't know what to print and where.
Main problem is that that I don't understand this. Simply got what you give me and used it. While I got errors, used quick fix. If there was more than one, tried all. And don't know whats going on. But I have to figure it out. I can't write milion lines in one class in future...
Hmm. Any guide about that? I don't even know what to search for...Last edited by Nozz; 04-25-2014 at 11:48 PM.
- 04-25-2014, 11:51 PM #13
Re: NullPointerException and ArrayIndexOutOfBoundsException...
How many instances of the GuiParts class are created?
Does the code add values to one instance
and then try to get those values from another instance?
To get the values from the instance of the class that has the values in it, the code needs a reference to that instance.
It can NOT create a new instance to get values from the first instance.
Change the code so that it only creates one instance of the GuiParts class and passes the reference to that object to whereever it is needed.If you don't understand my response, don't ignore it, ask a question.
- 04-26-2014, 12:28 AM #14
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
Re: NullPointerException and ArrayIndexOutOfBoundsException...
I can't catch it.
Is it create instance of GuiParts?
GuiParts gui = new GuiParts(program);
In Program I use "this" as argument, so I can use "this"?
In Players I use program or null. Don't know why... Just quick fix...
So how can I create just one instance?
I think I can understand what instance is, but I don't know which part of the code makes what...
So. I deleted this:
GuiParts gui = new GuiParts(program);
gui.send
GuiParts.send.setEnabled(true);
Oh, aslo forgot. Included some statics:
JButton getPlayers;
static JButton send;
static JTextArea playersList;
JTextArea messagesLog;
[/edit]
I'm not sure if it fixed this problem, because as I said, there is more mess with program instances(I think?).
Its little hard to figure out some things just by forums or tutorials.
Since you can't point me sometihng on my screen, maybe you can provide some code examples in posts? Just like in previous topic? I can't remember, or can't understand everything...
And please tell me one more thing. I'm not most mindless person here? Or am I? Because I'm little dissapointed in this what I'm doing here...
Exception in thread "AWT-EventQueue-0" program: null | GuiParts.send: javax.swing.JButton[,419,187,150x50,disabled,alignmentX=0.0,alignmentY =0.5,border=javax.swing.plaf.BorderUIResource$Comp oundBorderUIResource@750893d7,flags=296,maximumSiz e=,minimumSize=,preferredSize=,defaultIcon=,disabl edIcon=,disabledSelectedIcon=,margin=javax.swing.p laf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=START,defaultCapable=true]
java.lang.NullPointerException
at com._2nozz.Players.saveListState(Players.java:81)
at com._2nozz.Players.createPlayersTable(Players.java :69)
at com._2nozz.Players.getPlayersList(Players.java:29)
at com._2nozz.Program.actionPerformed(Program.java:24 )
Java Code:public void saveListState() { System.out.println("program: "+program+" | GuiParts.send: "+GuiParts.send); GuiParts.send.setEnabled(true); gotList = true; program.repaint(); -- line 81 }
Last edited by Nozz; 04-26-2014 at 12:33 AM.
- 04-26-2014, 12:48 AM #15
Member
- Join Date
- Apr 2014
- Posts
- 49
- Rep Power
- 0
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Ok. I think I figured it out.
Changed:
Players p = new Players(null);
Players p = new Players(this);
And... I did it accidentally while tried something else :P
- 04-26-2014, 12:57 AM #16
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Java Code:Players p = new Players(this);
If you don't understand my response, don't ignore it, ask a question.
- 04-26-2014, 05:58 AM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: NullPointerException and ArrayIndexOutOfBoundsException...
Build a wall around Donald Trump; I'll pay for it.
Similar Threads
-
ArrayIndexOutOfBoundsException
By spk in forum New To JavaReplies: 6Last Post: 09-02-2013, 07:33 PM -
ArrayIndexOutOfBoundsException Help
By latooka in forum New To JavaReplies: 1Last Post: 10-01-2012, 03:36 AM -
ArrayIndexOutOfBoundsException: 10 ??
By Yakg in forum New To JavaReplies: 2Last Post: 12-04-2010, 07:56 PM -
ArrayIndexOutOfBoundsException: 0
By mxsar in forum New To JavaReplies: 3Last Post: 11-16-2010, 11:59 PM -
ArrayIndexOutOfBoundsException
By flaskvacuum in forum New To JavaReplies: 6Last Post: 07-14-2009, 06:36 PM
Bookmarks