3

I've got a JFrame and automatically a content pane generated by Eclipse.

public JPanel contentPane = new JPanel();
public static Game frame;

The main method creates the new frame:

frame = new Game();
frame.setVisible(true);

Creating the new instance:

public Game() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    contentPane.setLayout(null);
    contentPane.setSize(500, 500);
    setContentPane(contentPane);

At the end of this I want to create a new object of my Field.java (which extends JLabel)

new Field(50, 50, 64, 64);

Field.java:

public Field(int x, int y, int x2, int y2) {
    setBounds(x, y, x2, y2);
    Game.frame.contentPane.add(this);
}

I hope you can understand what I'm trying to do. When adding the field to the contentPane of the Game class, I get a NullPointerException. I think, contentPane is null. But why? And what could I do to avoid this?

Error message:

java.lang.NullPointerException
at Hackbaellchen.Field.<init>(Field.java:23)
at Hackbaellchen.Lemmings.<init>(Game.java:73)
at Hackbaellchen.Lemmings$1.run(Game.java:27)

Field.java:23 is Game.frame.contentPane.add(this); Game.java:73 is new Field(50, 50, 64, 64);

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
  • 2
    can u give the exact error message, Where exactly which line whic hobject is having nullpointer. it helps in debugging – Kumar Saurabh Aug 31 '15 at 12:39
  • 1
    frame is static so in your program order executuion Game.frame.contentPane.add(this); is getting called first. make it non static or define the object for it before calling it in short your program is terminated before calling thre constructor of game class – Kumar Saurabh Aug 31 '15 at 12:41
  • 5
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – xlecoustillier Aug 31 '15 at 12:42
  • There's now an error message. –  Aug 31 '15 at 12:59
  • What's the Game.java:27 line? – Maslor Aug 31 '15 at 13:00
  • 1
    1) For better help sooner, post a [MCVE]. 2) See also [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 31 '15 at 13:11

2 Answers2

1

The solution is not to try to back-reference the Game instance from the Field's constructor. This kind of logic is bad practice anyway and here it tries to access the static field before it is set (from within the codepath of the Game constructor).

You should first create the Field instance, then assign it to Game from a point in code where it's known to exist. So move the line

Game.frame.contentPane.add(this);

to such a place (and replace this with the name of the Field variable).

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Like that (in Game class): contentPane.add(new Field(50, 50, 64, 64)); I can't see this label on my frame.. –  Aug 31 '15 at 13:07
0

From the information you have provided, without knowing the exact error stack trace, I think that the origin of your NullPointerException is at:

contentPane.setLayout(null);

When later on you call the Game instance, having that null field throws the exception.

Maslor
  • 1,821
  • 3
  • 20
  • 44
  • This is just the layout.. I prefer using coordinates.. are you sure? –  Aug 31 '15 at 12:58
  • Although I can't be sure since the information you provided is somewhat incomplete, I mostly get nullPointerExceptions from this kind of declarations. @Hackbaellchen – Maslor Aug 31 '15 at 13:01