0

This is a debug oriented question, but i've been very confused why some of my objects are returning NULL values.

I have a GUI here that takes strings from the text field when a button is clicked,

public class USAdditionalGUI extends javax.swing.JFrame {

public String AreaCode; // declare Strings
public String Exchange;
public String LastFour;
public String State;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // On button click use getText() to collect the strings from the 
    //`enter code here` fields and store them into the declared strings. 

    AreaCode = jTextField3.getText();
    Exchange = jTextField1.getText();
    LastFour = jTextField2.getText();
    State    = jTextField4.getText();

}      

The next class has get methods for each of the String varibles, and here lies the problem, I create an object to the GUI class and try to access the String information but it keeps coming up NULL when I print it out.

public class TRFUSAddressFormatting{
private String State;
private String areacode; // 3 digits
private String digitExchange; // 3 
private String lastfour; // 4

USAdditionalGUI usobj = new USAdditionalGUI();

public String getState(){

    State = usobj.State;
    System.out.println(State); // Why does this print NULL!!?!?!
    return State;
}


}                         

Am I accessing the string correctly?

1
  • It seems you don't really understand when your methods are called. Add traces to the code, or use a debugger and put a breakpoint at the beginning of each method, to understand how your code works. When getState() is called, the button 1 has not been clicked yet, or it has been clicked on another USAdditionalGUI instance. Commented Nov 15, 2013 at 16:54

2 Answers 2

2

when you declare strings, you need to initialize them or they will be null.

public class USAdditionalGUI extends javax.swing.JFrame {

    public String AreaCode = ""; // declare Strings
    public String Exchange = "";
    public String LastFour = "";
    public String State = "";

or whatever DEFAULT value you want to them

btw, I suggest you to declare them private and implement a get method to retrieve their information

public class USAdditionalGUI extends javax.swing.JFrame {

    private String AreaCode = ""; // declare Strings
    private String Exchange = "";
    private String LastFour = "";
    private String State = "";

    public String getAreaCode(){
         return AreaCode;
    }

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

1 Comment

Thanks for that, I didn't know that about strings in Java. I for sure will add the extra encapsulation as well.
0

Variables/fields in your snippet ie, AreaCode, Exchange are instance variables of a class which are initialized to their default values when class's object is created. Objects' default value is null unless explictly initialized and primitives have their own default values like int -> 0 etc. Since Areacode, Exchange, LastFour, State above are of type String which are stored as objects on JVM Heap hence they are initiazed to null unless you explicitly initialize them yourself.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.