简体   繁体   中英

Why is the monthString returning null

I'm parsing a bunch of ints from strings that were 'substring-ed' from a simpleDateFormat string. For some reason that I haven't been able to figure out the String monthString keeps coming up NULL, I dont see why.

I have created the string in another function call and when I pass it to the next function the string then becomes NULL

public class MainMenu extends JFrame implements ActionListener {
    private JButton start, highscore, help, stoppen;
    private int yearNumber, monthNumber, dayNumber, daysInMonth;
    private String monthString, yearString, dayString;

    private String timeStamp = new SimpleDateFormat("dd.MM.yyyy").format(Calendar.getInstance().getTime());



public void go(){
    setMonthString();
    getDayString();
    getYearString();
    getDayNumber(dayString);
    getYearNumber(yearString);
    getMonthNumber(monthString);
    getDaysInMonth(monthString);
    makeThisMonthFolders();
    maakComponenten();
    maakLayout();
    toonFrame();
}

private void makeComponent() {
    String timeStamp = new SimpleDateFormat("dd.MM.yyyy").format(Calendar.getInstance().getTime());

    String dayString = timeStamp.substring(0,2);

    System.out.println("Today is day " + dayNumber + " of the month");

    String monthString = timeStamp.substring(3, 5);

    System.out.println("Month string: " + monthString);


    start = new JButton("Move Folders"){
        {
            setSize(150, 75);
            setMaximumSize(getSize());
        }
    };
    start.addActionListener(this);
}

private String setMonthString(){
    String monthString = timeStamp.substring(3, 5);

    if(monthString.substring(0,1) == "0"){
        System.out.println(monthString.substring(0,1));
    }
    /*if(monthString.substring(0, 1) == "0"){
        monthString = monthString.substring(1);
    }*/
    return monthString;
}


public int getMonthNumber(String monthString){
    System.out.println(monthString);
    monthNumber = parseInt(monthString);
    return monthNumber;
}

}

Here is the null return from a test and the error codes thrown:

Today is day 0 of the month
Month string: 07
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at com.company.MainMenu.getMonthNumber(MainMenu.java:123)
at com.company.MainMenu.go(MainMenu.java:37)
at com.company.Main.main(Main.java:11)
7
null

ERROR AT 123 references:
System.out.println(monthString);
monthNumber = parseInt(monthString); // this line
return monthNumber;

Here you have called the function getMonthNumber(monthString); But you've not declared it globally. You need to declare the String monthString = timeStamp.substring(3, 5); outside the setMonthString() and then you will able to get the monthString value as needed.

I've posted below sample code of your's example so you can understand it better way.

public class MainMenu extends JFrame implements ActionListener {
    private JButton start, highscore, help, stoppen;
    private int yearNumber, monthNumber, dayNumber, daysInMonth;
    private String yearString, dayString;

    private String timeStamp = new SimpleDateFormat("dd.MM.yyyy").format(Calendar.getInstance().getTime());


public void go(){
    String monthString = getMonthString();
    getDayString();
    getYearString();
    getDayNumber(dayString);
    getYearNumber(yearString);
    getMonthNumber(monthString);
    getDaysInMonth(monthString);
    makeThisMonthFolders();
    maakComponenten();
    maakLayout();
    toonFrame();
}

private void makeComponent() {
    String timeStamp = new SimpleDateFormat("dd.MM.yyyy").format(Calendar.getInstance().getTime());

    String dayString = timeStamp.substring(0,2);

    System.out.println("Today is day " + dayNumber + " of the month");

    String monthString = timeStamp.substring(3, 5);

    System.out.println("Month string: " + monthString);


    start = new JButton("Move Folders"){
        {
            setSize(150, 75);
            setMaximumSize(getSize());
        }
    };
    start.addActionListener(this);
}

private String getMonthString(){
    String monthString = timeStamp.substring(3, 5);

    if(monthString.substring(0,1) == "0"){
        System.out.println(monthString.substring(0,1));
    }
    /*if(monthString.substring(0, 1) == "0"){
        monthString = monthString.substring(1);
    }*/
    return monthString;
}


public int getMonthNumber(String monthString){
    System.out.println(monthString);
    monthNumber = parseInt(monthString);
    return monthNumber;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM