Results 1 to 16 of 16
Like Tree6Likes
  • 1 Post By Norm
  • 1 Post By Tolls
  • 1 Post By jim829
  • 1 Post By gimbal2
  • 1 Post By Tolls
  • 1 Post By gimbal2

Thread: NullPointerException again?

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

    Default NullPointerException again?

    Hey,
    I got this, NullPointerException, again.
    But... I checked almost everything, there is nothing that can return null.
    And... What's weirdest here, everything works fine. Button is created, added(where exception hits) and works as it should.

    Should I care about this, or if it works, I can let it go with error?

    Project if you want to check it:
    https://github.com/N0zz/OTAdmin/blob...ersOnline.java
    Error:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at nozz.com.gui.GuiPlayersOnline.createButtons(GuiPla yersOnline.java:56)
    at nozz.com.windows.PlayersOnline.<init>(PlayersOnlin e.java:19)
    at nozz.com.windows.MainWindow.actionPerformed(MainWi ndow.java:26)
    ...
    Can you help me?
    Ehh, I wish to get more time for this, in last few days I just took one project and refactored it to this one to make it better...

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: NullPointerException again?

    Can you post the code so that the line numbers match the exception?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: NullPointerException again?

    You have github link.
    Line matches exception...
    Its here:
    playersOnlineWindow.add(getPlayerList);
    I didn't touched anything in this file now, so...

    Anyway. Here:
    Java Code:
    package nozz.com.gui;
    
    import java.awt.*;
    
    import javax.swing.*;
    
    import nozz.com.scripts.PlayersOnlineScripts;
    import nozz.com.windows.*;
    
    public class GuiPlayersOnline {
    
    	public static JButton getPlayerList;
    	public JFrame playersOnlineWindow;
    	public static JTextArea serverChoose, playersList, playersCountTitle;
    	public JScrollPane playersScroller;
    
    	private PlayersOnline playersonline;
    
    	public GuiPlayersOnline(PlayersOnline playersonline) {
    		this.playersonline = playersonline;
    	}
    
    	public void createPlayersOnlineWindow() {
    
    		EventQueue.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				playersOnlineWindow = new JFrame("Players Online List");
    				playersOnlineWindow
    						.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    				try {
    					UIManager.setLookAndFeel(UIManager
    							.getSystemLookAndFeelClassName());
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    				JPanel playersOnlinePanel = new JPanel();
    				playersOnlinePanel.setLayout(null);
    				playersOnlineWindow.setSize(300, 520);
    				playersOnlineWindow.setLocationByPlatform(true);
    				playersOnlineWindow.setResizable(true);
    				playersOnlineWindow.setVisible(true);
    				playersOnlineWindow.setLayout(null);
    
    				createButtons();
    				createTextAreas();
    			}
    		});
    
    	}
    
    	public void createButtons() {
    		getPlayerList = new JButton("Get Player List");
    		getPlayerList.setBounds(140, 435, 120, 25);
    		getPlayerList.addActionListener(playersonline);
    		playersOnlineWindow.add(getPlayerList);
    	}
    
    	public void createTextAreas() {
    		playersList = new JTextArea();
    		playersScroller = new JScrollPane(playersList);
    		playersScroller.setBounds(10, 45, 255, 380);
    		playersList.setEditable(false);
    		playersList.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    		playersOnlineWindow.add(playersScroller);
    
    		serverChoose = new JTextArea();
    		serverChoose.setBounds(10, 435, 120, 25);
    		serverChoose.setEditable(true);
    		serverChoose.append("vestia.pl");
    		serverChoose.setFont(new Font("Times New Roman", Font.BOLD, 16));
    		playersOnlineWindow.add(serverChoose);
    		
    		playersCountTitle = new JTextArea();
    		playersCountTitle.setBounds(40, 10, 210, 25);
    		playersCountTitle.setEditable(false);
    		playersCountTitle.append("Liczba graczy online: "+PlayersOnlineScripts.nickCounter);
    		playersCountTitle.setFont(new Font("Times New Roman", Font.BOLD, 18));
    		playersOnlineWindow.add(playersCountTitle);
    
    	}
    
    	public void createDropMenu() {
    
    	}
    
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: NullPointerException again?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at nozz.com.gui.GuiPlayersOnline.createButtons(GuiPla yersOnline.java:56)
    at nozz.com.windows.PlayersOnline.<init>(PlayersOnlin e.java:19)
    at nozz.com.windows.MainWindow.actionPerformed(MainWi ndow.java:26)
    What variable on line 56 has a null value? Print out the variables to see.

    The posted code does not match the stack trace. The stack trace shows that the actionPerformed() method at line 26 created an instance of the PlayersOnLine class . In that class's constructor at line 19 it called the createButtons() method.
    Last edited by Norm; 04-29-2014 at 03:31 PM.
    Nozz likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: NullPointerException again?

    Why is createButtons() public?
    Because I would assume that only the GuiPlayersOnline should control how and when buttons are added to the JFrame it owns.

    What's happening here is you are at some point (and the code in PlayersOnline would probably show this) calling createButtons before the JFrame has been created.
    Nozz likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: NullPointerException again?

    Quote Originally Posted by Nozz View Post
    You have github link.
    Quite a few of use are reluctant to visit off site locations. So posting the code is best. You didn't show all of your code so you must
    be calling createButtons before all initialization is done. It could also be related to thread timing.

    Regards,
    Jim
    Nozz likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: NullPointerException again?

    I can't see all the code (because I too am not going to dig through the GIT repository), but this bit of the stacktrace makes me a bit nervous:

    Java Code:
    nozz.com.windows.PlayersOnline.<init>
    Apparently the frame is created as part of the PlayersOnline constructor and that PlayersOnline instance is passed to the frame class, which will use it internally at a point where PlayersOnline might not have even been completely and fully initialized yet. I smell a code stink.
    Nozz likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: NullPointerException again?

    Print out the variables to see.
    I've printed everything in this line. Everything! No null. I was not a problem, but if it was, I could handle it because of it, I know, because there were some situations with this :P
    But not this time. Thats why I created this topic.

    Apparently the frame is created as part of the PlayersOnline constructor and that PlayersOnline instance is passed to the frame class, which will use it internally at a point where PlayersOnline might not have even been completely and fully initialized yet. I smell a code stink.
    Well, I'm not good at planning, I'm, also not experinced yet, so I know... My code can be ...

    You didn't show all of your code
    Yeah... but... it takes too long to copy more than 3 or 4 files... So...

    @@
    I realized that, that JTextAreas were created in same place, but didn't gave me any exceptions. So, with that what you guys told me, I moved "createButtons();" and its fixed.

    Thanks for help.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: NullPointerException again?

    I've printed everything in this line. Everything! No null.
    Post the println() statement and what it printed.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: NullPointerException again?

    But do you know exactly why moving the method fixed the problem? IMO, that is at least as important as fixing the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: NullPointerException again?

    Quote Originally Posted by Norm View Post
    Post the println() statement and what it printed.
    I don't remember for sure.
    For line:
    playersOnlineWindow.add(playersScroller);
    It was:
    System.out.println("x: "+playersOnlineWindow+"\nxx: "+playersScroller);
    Or something like this.
    And it printed something about playersOnlineWindow frame and playersScroller pane... So no null here...
    But maybe there was null? I did something wrong earlier? Maybe printed wrong thing in wrong place?
    ...
    Does it matter now?

    I tried now to print needed things in place where method was called, not where method was. There were null and null, so I think it was the problem.

    Quote Originally Posted by jim829 View Post
    But do you know exactly why moving the method fixed the problem? IMO, that is at least as important as fixing the problem.

    Regards,
    Jim
    I'm not sure, and I don't know how to explain what I think.



    There is little problem with everyone here. Its ok that you want to help, but sometimes you don't even look with what you should help, even if you have access to whole code. But... I also undestande, that getting to know whole project can take some time, and... you are here to help for nothing... Even if I'm little mad sometimes for some situations, I shouldn't... So sorry about that, you know, I need to accustom to this.
    But the problem is that, even if I try here to think, you don't tell me what I think good and what wrong. Even if I figure out something, I'm not sure in 100% how it works... And then I answer some questions about my guesses what fixed it and how it should be, just as in this post, and get nothing...
    I can work somehow with your tips and clues(and I know, its better if I do something, than if someone just give me done thing), I figure out something, but don't understand whole idea of it...
    Like now.

    Why there was exception and print in method didn't show nulls? Was it about nulls, where method was called?
    Or this wasn't problem?
    Maybe something about static's and public's in JButtons & areas, or in methods like createButton();
    Or using frame things outside method that was creating that frame?
    I think about first or third... Or both?
    But...
    If so, why it was able to create button? Even with nulls?
    *looked at code in 3rd post* And booom.
    Buttons method was called twice. Once good and once wrong... Thats reason of working button and errors at the same time?
    Do I think, or I don't have brain now?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: NullPointerException again?

    I'll explain, and some of this is a guess as the PlayerOnline code is missing here.

    Take the following code (from inside a PlayersOnline object):
    Java Code:
    GuiPlayersOnline gpo = new GuiPlayersOnline(this);
    at this point you have a GuiPlayersOnline instance, but the JFrame is null (look at the constructor).
    Java Code:
    gpo.createButtons();
    Since the JFrame in gpo is null the above call will cause your null pointer exception.

    It's possible your code does this instead:
    Java Code:
    gpo.createPlayersOnlineWindow();
    gpo.createButtons();
    which could still result in a null pointer exception, as the code in createPlayersOnlyWindow() is executed on a different thread (that's what the invokeLater() call does), so it's a real pot luck as to whether the JFrame is created or not by the time you call createButtons.

    Finally, since the createPlayersOnlineWindow method calls createButtons itself then there is absolutely no reason for the PlayersOnline class to be calling it. Hence my point that createButtons should be private, since it's something the Gui class should be handling and no one else.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: NullPointerException again?

    Thanks for explanation. I wasn't sure about that, but now I got it.

    createButtons should be private, since it's something the Gui class should be handling and no one else.
    Ok, but, how it affects program?
    It seems like it doesn't make any difference if it hase private or not.
    Well, if I try to use it somewhere else, maybe it does, so then I would use public. But if not, why should it be private? Does it change anything if its not private?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: NullPointerException again?

    It makes a difference when you develop.
    It allows the compiler to flag things that you have declared shouldn't be allowed. In this case you shouldn't allow something outside of the Gui class to access the createButtons method. The compiler will throw an error if you do try to access it, allowing you to fix a potential bug. It's all about letting the compiler help you code.
    Nozz likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default Re: NullPointerException again?

    The important thing in my eyes is, that the implementation should be hidden in a class.
    That way other classes cannot depend on your implementation which simply allows you to modify the implementation.

    If you make something public, you might have used it in some other class. So when you change the implementation, you will break it.

    So instead of the direct manipulation, you could write a method (e.g. you want to disable or enable the button which is the Next button in a wizard). That way you defined a behaviour of your class.
    You can even go further. You could define an interface with that method. Now the code that wants to deactivate the Next no longer needs to know your class. It can handle all classes that implement that interface.
    That way you can reduce dependencies and stuff is even easier to test.

    Konrad

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: NullPointerException again?

    Quote Originally Posted by Tolls View Post
    It makes a difference when you develop.
    It allows the compiler to flag things that you have declared shouldn't be allowed. In this case you shouldn't allow something outside of the Gui class to access the createButtons method. The compiler will throw an error if you do try to access it, allowing you to fix a potential bug. It's all about letting the compiler help you code.
    Compiler? No, this is something that is fixed in your brain. If you hide implementation details you make it impossible for you to misunderstand the intentions and design of your own code.

    If you then go ahead and call a private method anyway so the compiler has to jump in and help you get your head straight, it's a clear sign you need to work on your observational skills :)
    kneitzel likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Need help with nullpointerexception
    By andrewmills in forum New To Java
    Replies: 7
    Last Post: 12-08-2012, 01:35 PM
  2. NullPointerException
    By jwl in forum New To Java
    Replies: 15
    Last Post: 10-26-2012, 02:16 AM
  3. NullPointerException
    By s0meb0dy in forum New To Java
    Replies: 3
    Last Post: 10-09-2010, 08:12 PM
  4. Why do I get a NullPointerException?
    By nessa203 in forum New To Java
    Replies: 5
    Last Post: 01-07-2010, 01:14 PM
  5. NullPointerException
    By JohnST in forum New To Java
    Replies: 13
    Last Post: 01-06-2010, 12:37 AM

Posting Permissions

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