Before you mark this post as duplicate and referance What is a NullPointerException, and how do I fix it?, I have read that and (I am still new to java and might not understand this correctly) can't seem to see how that is connected to my error.
I am trying to dispose a java swing JFrame and load another in it's place and have gotten this to work. Even though it is doing exactly what I want, it still gives me the java.lang.NullPointerException error. As far as I understand the code that the error is connected to is functioning as I want it to.
Here is my relevant code. If you need me to include any other code I did not think was necessary please let me know and I will edit.
EDIT
I removed all my previous code and have added mcve code for you.
package test;
public final class Test {
private static GUI1 gui1;
private static GUI2 gui2;
public static void main(String[] args) {
startGUI1();
}
public static void startGUI1() {
gui1 = new GUI1();
}
public static GUI1 getGUI1() {
return gui1;
}
public static void startGUI2() {
gui2 = new GUI2();
}
public static GUI2 getGUI2() {
return gui2;
}
}
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUI1 {
JFrame frame;
public GUI1() {
frame = new JFrame();
frame.setVisible(true);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.add(new Pane());
}
public class Pane extends JComponent {
public Pane() {
try {
Scanner read = new Scanner(new File("user.txt"));
if (read.nextInt() != 0) {
test.Test.startGUI2();
test.Test.getGUI1().frame.dispose();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(GUI1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
package test;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUI2 {
JFrame frame;
public GUI2() {
frame = new JFrame();
frame.setVisible(true);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.add(new Pane());
}
public class Pane extends JComponent {
JLabel label = new JLabel("GUI2");
JButton button = new JButton("Start Gui1");
public Pane() {
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(5, 5, 5));
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setStroke(new BasicStroke(5.0F));
}
}
}
this produces the same error