0
for(int x = 0;x<14;x++){
    day[x]= theSheet.changeLetters(day[x]);
}

public String changeLetters(String entering){
    if(entering.equalsIgnoreCase("a")){
        entering = "10";
    } else {
        if(entering.equalsIgnoreCase("b")){
            entering = "11";
        } else {
            if(entering.equalsIgnoreCase("c")){
                entering = "12";
            } else {
                if(entering.equalsIgnoreCase("d")){
                    entering = "13";
                } else {
                    if(entering.equalsIgnoreCase("e")){
                        entering = "14";
                    } else {
                        if(entering.equalsIgnoreCase("f")){
                            entering = "15";
                        } else {
                            if(entering.equalsIgnoreCase("g")){
                                entering = "16";
                            } else {
                                if(entering.equalsIgnoreCase("h")){
                                    entering = "17";
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return entering;
}

Says the error is here if(entering.equalsIgnoreCase("a")) and in the for loop I am using to run the method. I'm trying to change the letter put into the string into a number.

Can anyone explain to me where the error might be? I'm having trouble spotting the issue. it lets me enter the letters fine but it has an exception once it gets to this for loop and runs this method.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
  • 2
    what error message are you getting exactly ? – marc wellman Dec 06 '12 at 18:49
  • and please post compilable code as you have it in your source file or annotate it accordingly otherwise. – marc wellman Dec 06 '12 at 18:50
  • Exception in thread "main" java.lang.NullPointerException at TimeSheeting.changeLetters(TimeSheeting.java:8) at the "if(entering.equalsIgnoreCase("a"))") at TimeSheets.main(TimeSheets.java:33) at the for loop – user1883368 Dec 06 '12 at 18:56
  • are you trying to create something like `entering = String.valueOf(entering.toLowerCase().charAt(0)-'a'+10)`? – Pshemo Dec 06 '12 at 18:58
  • are the values in your array initialized? If yes, than you are not showing us the code responsible for the `NullPointerException`. – jlordo Dec 06 '12 at 18:58
  • Yes the code is being initialized in a for loop before that one using for(int x =1;x<8;x++){ day[x-1] = JOptionPane.showInputDialog("Enter hour pairs for day "+x +".\n Enter the first digit: "); day[x] = JOptionPane.showInputDialog("Enter the second digit: "); } the letters being put in the array are then changed into numbers using the for loop posted earlier. – user1883368 Dec 06 '12 at 19:04
  • which line is line 8? That's where NPE is happening. – jlordo Dec 06 '12 at 19:24
  • How big is your array? I think you might not be getting to the end of it, because it looks like you're overwriting part of it. If your array is of size 16 instead of 8, you might have some null entries. – Lincoded Dec 08 '12 at 15:56

3 Answers3

4

why don't you use

if (condition) {
    // ...
} else if (condition2) {
   // ...
} else if (condition3) {
   // ...
}
// and so on

to make your code more readable. Your nested conditions are a mess. If you fix them, you might as well fix the error (if it's in the part of code you showed us).

Also add

System.out.println("Entering = '" + entering "'");

at the beginnig of your method to see if really receives what you are expecting it to receive.

jlordo
  • 37,490
  • 6
  • 58
  • 83
3

Ok according to

Yes the code is being initialized in a for loop before that one using for(int x =1;x<8;x++){ day[x-1] = JOptionPane.showInputDialog("Enter hour pairs for day "+x +".\n Enter the first digit: "); day[x] = JOptionPane.showInputDialog("Enter the second digit: "); } the letters being put in the array are then changed into numbers using the for loop posted earlier.

You have a logic error. You are overwriting previous entries and not filling the array up to 14 items. So items after 8 are left null, thus the NullPointerException.

Try this:

String[] day = new String[14];

for( int i = 0; i < 14; i+=2 ) {
    day[i] = JOptionPane.showInputDialog("Enter hour pairs for day "+(i/2+1) +".\n Enter the first digit: ");
    day[i+1] = JOptionPane.showInputDialog("Enter the second digit: ");
}

As a bonus, you can simplify the if-else code with:

public String changeLetters( String entering ) {
    return String.valueOf(entering.toUpperCase().charAt(0) - 55);
}
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • for values outside of `a-h` the method should return the input, according to his current code. – jlordo Dec 06 '12 at 19:00
  • 1
    @jlordo I don't know for sure, maybe the OP didn't get to write the remaining 15013513 if elses. His problem seems to be NPE and the code he gave is then unrelated anyway. – Esailija Dec 06 '12 at 19:01
0

As @jlordo already stated avoid nested if-statements with such a depth.

As an alternative you might use a switch statement in combination with an enum like this - although the following approach consists of more code it is more general and better suitable to be extended (e.g. using all letter of the alphabet and beyond) and it would make the code much more readable and more appropriate with respect to what you want to express.

Letter letter;

if (letter.equals("a")) letter = Letter.a;
if (letter.equals("A")) letter = Letter.A;
// and so on ...

switch (letter) {
    case a : { 
        // do some code here ...
        break;
    }
    case A : { 
        // do some code here ...
        break;
    }
    // and so on ...
}

public enum Letter {
    a (1),
    A (2),
    b (3),
    B (4),
    c (5),
    C (6);
    // and so on

    private final int index;

    Letter(int i) {

        index = i;
    }

    public int getIndex () {

        return index;
    }
}

Note that if you're using Java 7 you can use the switch statement even without the enum since it accepts strings as well like this:

switch (entering) {

    case "a" : {

        // ...
    }

    // ...
}
marc wellman
  • 5,808
  • 5
  • 32
  • 59