I created a string palindrome program, but I am getting a Nullpointer exception.
Here is the code that I have written:
public class JavaApplication2 {
/**
* @param args the command line arguments
*/
char c[]={'i','c','i','c','i'};
String s = new String(c);
String s1=null;
int i,j;
char c1[];
public static void main(String[] args) {
//TODO code application logichere
JavaApplication2 ja=new JavaApplication2();
ja.palindrome();
ja.ans();
boolean b1= ja.ans();
System.out.println(b1);
}
public void palindrome()
{
for(i=s.length()-1,j=0;i>=0;i--)
{
c1[j]=s.charAt(i);
j++;
}
}
public boolean ans()
{
String s2= new String(c1);
if(s2.equals(s))
{
return true;
}
else
return false;
}
}
I can't figure out how to deal with it and exactly why this error is occuring.
The Problem is that char c1[];
is not inizalized. Therefore this line give you a nullpointer:
String s2 = new String(c1);
Inizalized the variable:
char c1[] = {};
Now the NullPointer is gone, but thats not the final version, you need to fill it with your logic.