0

Why do I get a NullPointerException when I try and run this on a label:

JLabel player1CurrentScore = new JLabel("" + matchPlay.returnPL1GamesWon(),
                                        JLabel.CENTER);

Is it because I cannot have two strings concatenated like this?

Ideally, I am trying to set the label as the score of the player so it can be incremented correctly as and when is needed.

Here is my Exception stackdump:

java.lang.NullPointerException
at GUI.makeFrame(GUI.java:71)
at GUI.<init>(GUI.java:28)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at bluej.runtime.ExecServer$3.run(ExecServer.java:808)
1
  • Aside: If matchPlay.returnPL1GamesWon() is numeric, the better way to do this in recent JVMs is String.valueOf(matchPlay.returnPL1GamesWon()) and to avoid ""+ to "stringize" a number. As others said, the NullPointerException MUST be from matchPlay being null. Commented Mar 16, 2009 at 14:50

4 Answers 4

3

Based on the information in the question: the reference matchPlay is null.

Updated: Given the info that matchPlay cannot be null, then the method that is called on matchPlay must be throwing the exception. Check the stack trace for the previous method call, should help to pinpoint the problem.

Sign up to request clarification or add additional context in comments.

2 Comments

it cant be tho becuase some of the other methods from matchPlay are working, i have the following private Game matchPlay <constructor> matchPlay = new Game() so how can it be null?
Ah. You didn't say that. In that case, see @Paul Tomblin's answer below.
3

There's nothing there that will cause a null pointer exception unless matchPlay is null or matchPlay.returnPL1GamesWon() throws a null pointer exception itself.

Update: Based on the fact that the exception is coming from GUI.makeFrame, I have to ask if you've actually got a graphic display? Is this a command line app, a Swing app, or an Applet? It looks like you're trying to create a JLabel without a graphics context.

4 Comments

I think if matchPlay.returnPL1GamesWon() returns null, it will just print 'null', not throw an NullPointerException?
Yes, what Brabster said. However, it's possible that matchPlay.returnPL1GamesWon() is itself throwing a NullPointerException.
if i new how to use this site i wouldo post the error message exactly <stacktrace or whatever its called> however i did leave a comment above saying how it cant be possible
@Roberto, you should be able to edit your question - just click the "edit" button below it.
1

Change to:

player1CurrentScore = new JLabel("" + matchPlay.returnPL1GamesWon(),
                                    JLabel.CENTER);

Moreover then you can add Jlabel variable in public class if error occurred,

    public class YourClass{
             private JLabel player1CurrentScore;
    }

Comments

0

i have managed to answer it,

it was the order of which i was assigning fields in the constructor

sorry for the bother

thanks everyone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.