jeudi 17 septembre 2020

Why does my code execute with a NullPointerExeception and can someone please help me with this

Method name: isLongWord()

Return type: boolean

Input parameter:

String So I am suppose to have a method that will compare the length of the input parameter with the instance variable str. If the input string has larger length, return true; otherwise return false. My Code Below

public class TestIF { 

public boolean bo; 
public boolean boo; 
public int iA; 
public int iB; 
public double dD; 
public String str; 

public boolean isLongWord(String n){ 

int b = n.length(); 
int z = str.length(); 

if (b > z){ 
return true;
 } 

else { 
return false; 
} 

} 
}


Then in bluej I create my object and enter the parameter with a string and then It gives me the java.lang.NullPointerException error and it highlights this piece of code if (b > z). I don't know how to fix this error or what to do can someone please help me.

How I ran the code

1: https://gyazo.com/93fbcb8f6dc8e851ae021dca443dee99

2: https://gyazo.com/4869c6c396777c764681c1de178ad371

3: https://gyazo.com/c629286280dc302d352d2e6bb928ad14

Aucun commentaire:

Enregistrer un commentaire