0

I'm trying to build a game and I have an error that comes up. I'm relatively new to coding so any help is appreciated.

I try to run the following in Java to set up a frame using the JFrame library, but end up getting an error. Thoughts on a solution?

package com.key.gameproject;

import javax.swing.JFrame;

public class MainClass {

public static void main(String args[]) {
    System.out.println("Test");
    JFrame frame = new JFrame();
    frame.setSize(640, 480);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}

Error:

Test
Exception in thread "main" java.lang.NullPointerException
    at java.awt.Window.init(Window.java:497)
    at java.awt.Window.<init>(Window.java:537)
    at java.awt.Frame.<init>(Frame.java:420)
    at java.awt.Frame.<init>(Frame.java:385)
    at javax.swing.JFrame.<init>(JFrame.java:189)
    at com.key.gameproject.MainClass.main(MainClass.java:9)

Update: I reinstalled Eclipse, Java and still running into the same issue. Tried in a different IDE and also ran through terminal (javac, java) and still nothing works

Update 3/11 - I'm trying to run this on Mac high sierra which results in the above error. However, on Windows it seems okay. javax.swing.JFrame (JFrame.java:189) looks exactly the same on both machines. Again, I have already reinstalled multiple times and appreciate the help so far (@camickr). The issue is still not resolved and I can't find a similar issue on Stack.

Daejichu
  • 57
  • 8
  • The error message tells you which class is causing the problem. It is NOT the "MainClass` so the code you posted doesn't help. The problem is in your "Game" class and it tells you the line number causing the problem. So look at the statement at that line and figure out which variable is null. If you are not sure what a NullPointerException is then search the forum for other questions with the same Exception to see how the problem was solved. – camickr Mar 10 '18 at 20:42
  • Also, it looks like you have created a class called "Window". Don't use that name since AWT already has a class with that name and it is confusing. A class name should be descriptive. – camickr Mar 10 '18 at 20:47
  • Thank you camickr! I accidently posted the wrong error when trying to debug myself. I reposted and I only have the MainClass.java class. – Daejichu Mar 10 '18 at 20:51
  • The posted code compiles and executes fine for me. However, the posted code and message don't match. The error says the problem is in `com.home.src.MainClass`, but the code you posted doesn't have a package statement so I don't know what code you are actually compiling. It seems to me like this is a problem with your IDE or classpath in the IDE, or you have some old file lying around that you are actually compiling. Since I don't use an IDE I'm not sure what to suggest. – camickr Mar 10 '18 at 21:06
  • `when trying to debug myself.` One thing to try is to add a `System.out.println("hello");` as the first statement in the main() method. This should display on the screen if you are in fact compiling and testing the posted code. Adding display statements is a simple debugging approach to help you understand the logic flow of the code. – camickr Mar 10 '18 at 21:07
  • Got it, okay I think I'll try and delete and restart my IDE. I'm using Eclipse and I did place the package name as well on the top of my file. I'll update once I try again. Appreciate the help camickr – Daejichu Mar 10 '18 at 21:10
  • Alright, restarted the project, restarted Eclipse and looks like it's reading my System.out.println("Test"), but is having an issue with JFrame frame = new JFrame(); – Daejichu Mar 10 '18 at 21:16
  • So you have verified that you are compiling the executing the posted code. I'm not sure what the problem could be. Maybe the IDE or Java isn't installed correctly? Try deleting all old class files lying around in your source directory? Can you create other Swing components, like a JLabel before you create the frame? Sorry, I can't offer any other advice. – camickr Mar 10 '18 at 21:27
  • @ejp please don't blindly close a question just because you see the words NullPointerException. Please take the time to read the question and the comments. Did you even try to execute the code? I for one have no problem executing the code. If you think a NPE is the real problem then please answer because I am curious what can cause a problem like this, because I have no idea and I don't see anything in the linked answer that addresses this issue. Please reopen the question so a valid answer can be provided. – camickr Mar 10 '18 at 23:15
  • @EJP please reopen and appreciate the help – Daejichu Mar 11 '18 at 23:13
  • `I'm trying to run this on Mac high sierra which results in the above error. However, on Windows it seems okay` - seems like this is a platform issue. I would suggest you try reposting the question with the above information at the top of your question. You might get more eyes on the question on Monday. Hopefully people won't be so quick to close the question. – camickr Mar 12 '18 at 01:36
  • However, one more thought before you do that. Make sure the GUI code is executed on the Event Dispatch Thread (EDT). Read the Swing tutorial on [Concurrency](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for more information. Try downloading and running the [FrameDemo.java](https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html) code. This creates the GUI properly on the EDT. Normally you shouldn't have problem for a simple class like this, but maybe your platform does have an issue when Swing guidelines aren't followed. – camickr Mar 12 '18 at 01:40
  • @camickr . That somehow made everything work! Now when I run through my program again on high sierra it's completely fine and I'm able to have the frame pop up. Thank you SO much for your help and solution! – Daejichu Mar 12 '18 at 13:50

0 Answers0