0
import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
import java.io.*;

public class Login  {
    private JFrame theWind;
    //private JPanel thePane;
    private JLabel title;
    private JButton ok;
    //private JButton cancel;
    private JTextField theText;

    public Login(){
    }

    public void open(){
        theWind = new JFrame("Login");
        title = new JLabel("Please Enter Your Voter ID:");
        ok = new JButton("OK");
        //cancel = new JButton("Cancel");
        theText = new JTextField();
        theWind.setLayout(new BorderLayout());
        theWind.add(title,BorderLayout.NORTH);
        theWind.add(theText,BorderLayout.CENTER);
        theWind.add(ok,BorderLayout.SOUTH);

        theWind.pack();

        theWind.setVisible(true);
    }
}

I apologize this isn't the complete programs so some of the variables are not used yet. So when i click a button on another JFrame, this JFrame is supposed to open up. The problem occurs when it hits the line that sets the value for the Jframe. (the code below).

theWind = new JFrame("Login");

I get a run time error claiming a NullPointerException on that line. I checked to see if the the JFrame was declared, and it is. This is weird considering that my other JFrame runs fine, and I have all of the necessary java libraries imported. I have a JPanel and a JButton commented because I was trying to figure out if those things were messing up with the borderlayout but I don't think that's the problem. It's a relatively short code and I'm really stumped on this. I would be glad to receive any inputs/advice. thank you!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
jojexy
  • 1
  • Sorry, but this, `theWind = new JFrame("Login");`, absolutely cannot be the line that throws the NullPointerException. But regardless, the answer to this question is the same as the answer to the umpteen million other NullPointerException questions. Voting to close as a duplicate. – Hovercraft Full Of Eels Nov 29 '15 at 02:49
  • The heuristic for solving NullPointerExceptions is almost always the same. **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You should re-check because almost undoubtedly, you're focusing on the wrong line. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Nov 29 '15 at 02:51
  • As a "side" recommendation, one that is unrelated to your problem at hand -- you probably don't want more than one JFrame displaying, and you certainly don't want to be swapping JFrames. To see why, please read: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Nov 29 '15 at 02:53
  • @HovercraftFullOfEels Hey Man. Thanks for the advice. It turns out that closing my first JFrame somehow made my second JFrame appear. Weird stuff, but I guess there was some sort of interaction? thanks anyways – jojexy Nov 29 '15 at 02:58
  • The only interaction is that which you've coded since it's a bug somewhere in your code and not in Java or the Swing library, but where I can't tell. Consider posting more code, and we'll likely have a better idea. – Hovercraft Full Of Eels Nov 29 '15 at 03:03
  • @HovercraftFullOfEels Actually I realized that my class files were not updating. (Normally when i compile the main file, it will also compile the login file). – jojexy Nov 29 '15 at 03:15

0 Answers0