为什么monthString返回null

时间:2019-07-04 05:10:59

标签: java string

我正在从simpleDateFormat字符串“子字符串化”的字符串中解析一堆整数。由于某种原因,我一直无法弄清字符串monthString不断上升为NULL,我不明白为什么。

我已经在另一个函数调用中创建了字符串,当我将其传递给下一个函数时,字符串将变为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;

1 个答案:

答案 0 :(得分:-1)

在这里您已经调用了函数getMonthNumber(monthString);,但尚未全局声明它。您需要在String monthString = timeStamp.substring(3, 5);外部声明setMonthString(),然后才能根据需要获取monthString值。

我已在您示例的示例代码下面发布了内容,以便您更好地理解它。

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;
}