Results 1 to 17 of 17
Like Tree5Likes
  • 1 Post By milovan
  • 1 Post By milovan
  • 1 Post By Norm
  • 1 Post By Norm
  • 1 Post By Norm

Thread: NullPointerException and ArrayIndexOutOfBoundsException...

  1. #1
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default 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://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();
    	}
    
    }
    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

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Try to print nickCounter after you change it to see what values it reaches and where it fires exception..
    Nozz likes this.

  3. #3
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default 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 09:49 PM.

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Ok, what method gives you exceptions, createPlayersTable() or printPlayers() or something else (I didn't look thoroughly)?
    Nozz likes this.

  5. #5
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Quote Originally Posted by milovan View Post
    Ok, what method gives you exceptions, createPlayersTable() or printPlayers() or something else (I didn't look thoroughly)?
    ...
    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";
    I thought there will be 515 names and then 985 something... Without nulls, no null exception? Idk.
    Last edited by Nozz; 04-25-2014 at 09:58 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com._2nozz.Players.saveListState(Players.java:77)
    What variable has the null value on line 77? Then backtrack to see why it is null.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default 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 )
    Line 81:
    gui.send.setEnabled(true);
    ...
    Is this good?
    http://scr.hu/0xu3/6hbxz
    Last edited by Nozz; 04-25-2014 at 10:11 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default 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.

  9. #9
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    please put your method saveListState() here to see how it looks now

  10. #10
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Java Code:
    System.out.println("gui: "+gui+" | gui.send: "+gui.send+" | gotlist: "+gotList+"");
    returned:
    Java Code:
    gui: com._2nozz.GuiParts@2aeb09a5 | gui.send: null | gotlist: false
    @edit
    Java Code:
    	public void saveListState() {
    	System.out.println("gui: "+gui+" | gui.send: "+gui.send+" | gotlist: "+gotList+"");
    		gui.send.setEnabled(true);
    		gotList = true;
    		program.repaint();
    	}
    gotList is now declared in Players. And in Program I used:
    if (p.gotList == true)
    And it works.
    Problem is now on gui.send... Why?
    Last edited by Nozz; 04-25-2014 at 10:21 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Problem is now on gui.send... Why?
    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.
    Nozz likes this.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default 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
    So program is a problem too :/

    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.
    In class GuiParts. I don't know where gui reffers to exacly.
    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 10:48 PM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default 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.
    Nozz likes this.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    I can't catch it.

    Is it create instance of GuiParts?
    GuiParts gui = new GuiParts(program);
    If so, there are 2 instances, one in Players, one in 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);
    And instead of:
    gui.send
    Used:
    GuiParts.send.setEnabled(true);
    [edit]
    Oh, aslo forgot. Included some statics:
    JButton getPlayers;
    static JButton send;
    static JTextArea playersList;
    JTextArea messagesLog;
    Just quick fix. But it works. I want to understand it too.
    [/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 )
    Code:
    Java Code:
    	public void saveListState() {
    		System.out.println("program: "+program+" | GuiParts.send: "+GuiParts.send);
    		GuiParts.send.setEnabled(true);
    		gotList = true;
    		program.repaint(); -- line 81
    	}
    I will try to figure out program null now. But any help will be good. Because program is made little more complicated here than guipart was...
    Last edited by Nozz; 04-25-2014 at 11:33 PM.

  15. #15
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Ok. I think I figured it out.
    Changed:
    Players p = new Players(null);
    To:
    Players p = new Players(this);
    I'm just not sure how "this" can do anything about paintcompontent, paint, repaint etc...
    And... I did it accidentally while tried something else :P

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Java Code:
    Players p = new Players(this);
    That statement passes a reference to the instance of the class where that statement was executed.
    Nozz likes this.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: NullPointerException and ArrayIndexOutOfBoundsException...

    Quote Originally Posted by Nozz View Post
    Java Code:
    public class Players {
    	private Program program;
    
    	public Players(Program program) {
    		this.program = program;
    	}
    
    	GuiParts gui = new GuiParts(program);
    This probably doesn't do what you want it to do: the GuiParts object is created with a null program (the constructor for the Players object hasn't run yet); there are more suspicious object initializers in your classes ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. ArrayIndexOutOfBoundsException
    By spk in forum New To Java
    Replies: 6
    Last Post: 09-02-2013, 06:33 PM
  2. ArrayIndexOutOfBoundsException Help
    By latooka in forum New To Java
    Replies: 1
    Last Post: 10-01-2012, 02:36 AM
  3. ArrayIndexOutOfBoundsException: 10 ??
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 06:56 PM
  4. ArrayIndexOutOfBoundsException: 0
    By mxsar in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 10:59 PM
  5. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •