NullPointerException

help or not your choice

Page 1 of 1

2 Replies - 934 Views - Last Post: 10 April 2007 - 08:37 AM Rate Topic: -----

#1 Storm8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-April 07

NullPointerException

Posted 09 April 2007 - 08:15 AM

Hey i need helpp with my code here it is:
package name;
import javax.swing.*;
import java.awt.*;

public class Main {
public static String name[][]=new String[100][2];	
	
	public static void main(String[] args) {
boolean repeatnames=true;
boolean search=true;
int s;
int b;
int i=0;
int n;
int w=0;
String mainm;
String searchnames;
String searchnick;
String addname;
String nam;
String nick;
String names;
String fname;
String m;


 do{
	  repeatnames=false;
	  mainm=JOptionPane.showInputDialog("What do you want to do?"+'\n'+"1. Enter Names"+'\n'+"2. Search For Names");
	  s=Integer.parseInt(mainm);
	  switch(s){
				   case 1:
												  names = JOptionPane.showInputDialog("What is the name you are entering?");
												  System.out.println(names);
												  nick = JOptionPane.showInputDialog("What is the nick name of this person?");
												  System.out.println(nick);
												  name[i][w]=names+nick;
												  i++;
												  System.out.println(name[i][w]);
						  repeatnames=true;
				   break;//end of case 1
				   
				   case 2:
						   searchnames = JOptionPane.showInputDialog("What Are you loking for?"+'\n'+"1.  name");
						   b=Integer.parseInt(searchnames);
						   switch(b){
										 case 1:
												  
												  fname = JOptionPane.showInputDialog("What is the name you are looking for?");
												  System.out.println(fname);
												  i=0;
												  search=true;
												  do{
													  if(name[i][1].equals(fname))
													  {
															  JOptionPane.showMessageDialog(null,"the nick name for "+fname+" is "+name[i][w]);
															  System.out.println(name[i][w]);
															  search=false;
															  repeatnames=true;
													  }
												  else
													  i++;
													 }while(search);
										  break;
										}
				   break;
				   }
}while(repeatnames);
	}
}



Right here i am getting a NullPointerException but dont know why
Please help
if(name[i][1].equals(fname))

thanks
Storm(yes thats my real name)

Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 251
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: NullPointerException

Posted 09 April 2007 - 09:56 AM

View PostStorm8, on 9 Apr, 2007 - 08:15 AM, said:

Hey i need helpp with my code here it is:
package name;
import javax.swing.*;
import java.awt.*;

public class Main {
public static String name[][]=new String[100][2];	
	
	public static void main(String[] args) {
boolean repeatnames=true;
boolean search=true;
int s;
int b;
int i=0;
int n;
int w=0;
String mainm;
String searchnames;
String searchnick;
String addname;
String nam;
String nick;
String names;
String fname;
String m;


 do{
	  repeatnames=false;
	  mainm=JOptionPane.showInputDialog("What do you want to do?"+'\n'+"1. Enter Names"+'\n'+"2. Search For Names");
	  s=Integer.parseInt(mainm);
	  switch(s){
				   case 1:
												  names = JOptionPane.showInputDialog("What is the name you are entering?");
												  System.out.println(names);
												  nick = JOptionPane.showInputDialog("What is the nick name of this person?");
												  System.out.println(nick);
												  name[i][w]=names+nick;
												  i++;
												  System.out.println(name[i][w]);
						  repeatnames=true;
				   break;//end of case 1
				   
				   case 2:
						   searchnames = JOptionPane.showInputDialog("What Are you loking for?"+'\n'+"1.  name");
						   b=Integer.parseInt(searchnames);
						   switch(b){
										 case 1:
												  
												  fname = JOptionPane.showInputDialog("What is the name you are looking for?");
												  System.out.println(fname);
												  i=0;
												  search=true;
												  do{
													  if(name[i][1].equals(fname))
													  {
															  JOptionPane.showMessageDialog(null,"the nick name for "+fname+" is "+name[i][w]);
															  System.out.println(name[i][w]);
															  search=false;
															  repeatnames=true;
													  }
												  else
													  i++;
													 }while(search);
										  break;
										}
				   break;
				   }
}while(repeatnames);
	}
}



Right here i am getting a NullPointerException but dont know why
Please help
if(name[i][1].equals(fname))

thanks
Storm(yes thats my real name)

The problem is that you're trying to cal the toString method on null. After adding one name to the array and then searching for that name, I noticed that the name was added to name[0][0], but your code tries to find it at name[0][1], which is empty. First of all, you don't need to use a two-dimensional array. Just stick with one-dimensional array and you'll have much more luck with what it appears you are trying to do. You and Donnie16 appear to be working on the same problem, so let me give you both some tips on iterating through an array.

1. don't use do-while. Use a for loop. There are two ways to do this.

// old style for loop

for ( int i = 0; i < names.length; i++ ) {
   String name = names[i];
   if ( name != null ) {
	  // do stuff
   }
}

// enhancedfor loop in Java 5.0 and later )
for ( String name : names ) {
   if ( name != null ) {
	  // do stuff
   }
}


A for loop will iterate through all values of the array, so no flags are probably necessary in this situation. Maybe you and Donnie16 can give us a better idea of what your assignment is, so our advice will be better. Also, what class you are taking would be helpful as well.
Was This Post Helpful? 0
  • +
  • -

#3 Storm8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-April 07

Re: NullPointerException

Posted 10 April 2007 - 08:37 AM

Alright new plan.

I got the program to write and read the array.

now i have to add another boolean to make it ask if they want to add the name and nick name if not found.
searchnames = JOptionPane.showInputDialog("What Are you loking for?"+'\n'+"1.  name"+'\n'+"2.  nick name");
					b = Integer.parseInt(searchnames);
					switch(b)
					{
						case 1:
							fName = JOptionPane.showInputDialog("What is the name you are looking for?");
							System.out.println(fName);
							i=0;
							search=true;
							do
							{
								if(name[i][0].equals(fName))
								{
									JOptionPane.showMessageDialog(null,name[i][0]+" "+name[i][1]);
									System.out.println(name[i][w]);
									search=false;
									repeatnames=true;
								}
								else
									i++;
							}while(search);
						break;


and here
						case 2:
							nName = JOptionPane.showInputDialog("What is the nick name you are looking for?");
							System.out.println(nName);
							i=0;
							search=true;
							do
							{
								System.out.println(name[i][w]);
								if(name[i][1].equals(nName))
								{
									JOptionPane.showMessageDialog(null,name[i][w]);
									System.out.println(name[i][0]+" "+name[i][1]);
									search=false;
									repeatnames=true;
								}
								else
									i++;
							}while(search);



and a variable that will keep track of how many names are in the array.
this is going to get confusing.

This post has been edited by Storm8: 10 April 2007 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1