-2

I have the following code-

if (aa[0].equals("add")) {
    if (aa[1].equals("s")&aa[3].equals("s")) {
        A= Double.parseDouble (UISAVE[Integer.parseInt(aa[2])]);
        B= Double.parseDouble (UISAVE[Integer.parseInt(aa[4])]);
    }
    if (aa[1].equals("s")&!aa[3].equals("s")) {
        A= Double.parseDouble (UISAVE[Integer.parseInt(aa[2])]);
        if (aa[3].equals(null)) {
            aa[3]="0";
        }
        B= Double.parseDouble (aa[3]);
    }
    if (!aa[1].equals("s")) {
        A= Double.parseDouble (aa[2]);
        B= Double.parseDouble (aa[3]);
    }
    out[count]=Double.toString(A+B);
    read(in, out, UISAVE);
}

and I get this error -

Exception in thread "main" java.lang.NullPointerException at CMDL.shizzle.april2014.Write.read(Write.java:25)

Now, as it says the error is apparently an if statement... if (aa[1].equals("s"(&aa[3].equals("s")) { and I've tried most of what I can do and haven't found a solution yet.

Thanks if you can help.

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
imthegman55
  • 80
  • 1
  • 9
  • Either aa[1] or aa[3] is null, but that's about all I can tell you based on the information given – Kevin DiTraglia May 10 '14 at 02:39
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – yshavit May 10 '14 at 02:40

1 Answers1

0

Update your if statements as follows:

if (aa[0].equals("add")) {
    if ((aa[1].equals("s")) && (aa[3].equals("s"))) {
        A= Double.parseDouble (UISAVE[Integer.parseInt(aa[2])]);
        B= Double.parseDouble (UISAVE[Integer.parseInt(aa[4])]);
    }
    if ((aa[1].equals("s")) && (!aa[3].equals("s"))) {
        A= Double.parseDouble (UISAVE[Integer.parseInt(aa[2])]);
        if (aa[3].equals(null)) {
            aa[3]="0";
        }
        B= Double.parseDouble (aa[3]);
     }
    if (!aa[1].equals("s")) {
        A= Double.parseDouble (aa[2]);
        B= Double.parseDouble (aa[3]);
    }
    out[count]=Double.toString(A+B);
    read(in, out, UISAVE);
}

the problem is you are currently doing a bitwise &, so you need to update it to use a conditional-and && instead.

Also, before accessing array indices like that, you may want to check whether or not the array has enough elements inside it...

if (aa.length >= 3)
{
    ... 
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82