-2

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;
NDC
  • 1
  • 3
  • `monthString.substring(0,1) == "0"` - no, use `equals` – Scary Wombat Jul 04 '19 at 05:12
  • @ScaryWombat indeed, that is a mistake, but that is not causing the problem here. – Stultuske Jul 04 '19 at 05:28
  • @NDC your code uses variables that don't exist. please post your actual code, not something that is "somewhat similar" – Stultuske Jul 04 '19 at 05:29
  • Okay I changed it to equals now. Is now updating the string, however for some reason will not save the monthNumber variable value when I parse it from the month String – NDC Jul 04 '19 at 05:32
  • The return value of `setMonthString();` is not being assigned to anything – Scary Wombat Jul 04 '19 at 05:34
  • @NDC setters are supposed to set the value of a variable that 'll contain the value after the method is finished executing. local variables only exist during execution of the method – Stultuske Jul 04 '19 at 05:35
  • But in your go method, you pass variables that don't exist as parameters to methods, so how on earth did you ever get that to compile? – Stultuske Jul 04 '19 at 05:36
  • sorry, i added the code now with the declared variables that I was passing to the method calls – NDC Jul 04 '19 at 05:44
  • and where is `parseInt` defined? – Scary Wombat Jul 04 '19 at 05:44
  • The Code which is accepting the monthString is null, because the line which calls getMonthNumber(monthString); is passing the instance variable of Object which is null public int getMonthNumber(String monthString){ System.out.println(monthString); monthNumber = parseInt(monthString); return monthNumber; } Update this code to accept the proper value, by storing the month value inside the instance variable monthString – Manjunath H M Jul 04 '19 at 05:46
  • You shouldn't use `SimpleDateFormat` and `Calendar`, since they're obsolete. Use the newer Java Date and Time API from the `java.time` package. – MC Emperor Jul 04 '19 at 06:02
  • And don't call methods functions. Call them methods. – MC Emperor Jul 04 '19 at 06:04

1 Answers1

-1

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;
}
Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
  • This definitely works. Would it be better to write a separate function that will return the monthString and pass it the returned value of the simpleDateFormat as a parameter to the method? I'm a novice but would'nt it be better to keep that out of a global scope? – NDC Jul 04 '19 at 05:51
  • Yes, You can make it better by Creating the simple architecture of your program. I've updated the answer with the return of month. – Dushyant Tankariya Jul 04 '19 at 07:33