Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: NullPointerException

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException

    Good evening all, I'm trying to basically enter String data into my array then int data into another matrix array and then ask the user thru swing what they want to delete from the String array, but I get the error NullPointerException as soon as I enter the word I want to delete... here's my code.. I'm not done yet but I want to know why the mistake is there..
    import javax.swing.*;
     
     
    public class TrabajoFinal
    {
    	public static void main(String[]args)
    	{
    		int opc=0;
    		do
    		{
    		String op=JOptionPane.showInputDialog(null, "1.	Ingresar nuevo municipio\n2.-Borrar un municipio\n3.Consultar información de un municipio\n4.Modificar información de un municipio\n5.Generar Reporte\n6.Salir\nQue opcion deseas?"
    		);
    		opc=Integer.parseInt(op);
    		String mun[]=new String[10];
    		int dat[][]=new int[10][10];
    		String ind[]=new String[10];
    		switch(opc)
    		{
    			case 1:IngresarMunicipio(mun, dat);break;
    			case 2:BorrarMunicipio(mun, dat);break;
    			case 3:ConsultarInfo(mun);break;
    			case 4:
    			case 5:
    			case 6:JOptionPane.showMessageDialog(null, "Adios");break;
    			default: JOptionPane.showMessageDialog(null, "Opcion invalida.");break;
    		}
    		} while(opc!=6);
    	}
     
    	public static void IngresarMunicipio(String m [], int i[][])
    	{
    		String d=(" ");
    		int cont=0;
    		for(int w=0;w<10;w++)
    		{
    		if(m[w]==null)
    		{
     
     
    		m[w]=JOptionPane.showInputDialog(null, "Cual es el nombre del municipio?");
    		JOptionPane.showMessageDialog(null, "Municipio Ingresado: "+m[w]);
     
     
    		int opc=0;
    		cont=w;
    		do
    		{
    		String op=JOptionPane.showInputDialog(null, "1.-Poblacion total\n2.-Matrimonios\n3.-Divorcios\n4.-Nacimientos\n5.-Hogares con jefatura femenina\n6.-Tasa de alfabetización de las personas de 15 a 24 años\n7.-Familias beneficiarias por el Programa de Desarrollo Humano Oportunidades\n8.-Población de 5 y más años con primaria\n9.-Población de 18 años y más con nivel profesional\n10.-Delitos registrados en averiguaciones previas del fuero común\n11.-Salir\nDame el numero de indicador que deseas cambiar/agregar._");
    		opc=Integer.parseInt(op);
    		if(opc==11)
    			JOptionPane.showMessageDialog(null, "Ingreso de datos concluidos.");
    		else
    		{
    		d=JOptionPane.showInputDialog(null, "Cual es el valor de el indicador "+opc+"?");
    		i[cont][opc-1]=Integer.parseInt(d);
    		}
    	} while(opc!=11);
    		} break;
    		}
     
    	}
     
    	public static void BorrarMunicipio(String m[], int i[][])
    	{
    		String b=JOptionPane.showInputDialog(null, "Que municipio deseas borrar?");
    		int matchIndex=-1;
     
     
    			for(int w=0;w<10;w++)
    			{
    				if(m[w].equals(b))
    				{
    					String a=JOptionPane.showInputDialog(null, "Municipio: "+m[w]+"\n"+"Indicador 1: "+i[w][0]+"\n"+"Indicador 2: "+i[w][2]+"\n"+"Indicador 3: "+i[w][2]+"\n"+"Indicador 4: "+i[w][3]+"\n"+"Indicador 5: "+i[w][4]+"\n"+"Indicador 6: "+i[w][5]+"\n"+"Indicador 7: "+i[w][6]+"\n"+"Indicador 8: "+i[w][7]+"\n"+"Indicador 9: "+i[w][8]+"\n"+"Indicador 10: "+i[w][9]+"\n"+"Estas seguro que lo quieres borrar?\n1.-SI\n2.-NO");
    					int d=Integer.parseInt(a);
    					if(d==1)
    					{
    						m[w]=(" ");
    						JOptionPane.showMessageDialog(null, "Municipio Borrado");
    						matchIndex=w;break;
    					}
    					else
    						JOptionPane.showMessageDialog(null,"Municipio no borrado.");
     
    				}
    			}
     
     
     
     
     
     
     
     
    	}
     
    	public static void ConsultarInfo(String m [])
    	{
     
    	}
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: NullPointerException

    Can you give us the full stack trace.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Sorry kind of new.. what's a full stack trace?

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: NullPointerException

    The console doesn't just say 'NullPointerException'. It will give you a much larger message called a stack trace. It will look something like this:

    Exception in thread "main" java.lang.NullPointerException
    at com.example.myproject.Book.getTitle(Book.java:16)
    at com.example.myproject.Author.getBookTitles(Author. java:25)
    at com.example.myproject.Bootstrap.main(Bootstrap.jav a:14)

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Oh right, here.
    --------------------Configuration: <Default>--------------------
    Exception in thread "main" java.lang.NullPointerException
    at TrabajoFinal.BorrarMunicipio(TrabajoFinal.java:72)
    at TrabajoFinal.main(TrabajoFinal.java:21)

  6. #6
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: NullPointerException

    As the stackTrace says please look at line no 72 in your program or post that here. I tried copying your code but due to different editor used line no 72 is pointing to someother line where exception can't occur.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Here's the whole method:
    Line 72 is ...
    if(m[w].equals(SearchKey))
     public static void BorrarMunicipio(String m[], int i[][])
    	{
    		String SearchKey=JOptionPane.showInputDialog(null, "Que municipio deseas borrar?");
    		int matchIndex=-1;
     
     
    			for(int w=0;w<10;w++)
    			{
    				if(m[w].equals(SearchKey))
    				{
    				matchIndex=w;
    					String a=JOptionPane.showInputDialog(null, "Municipio: "+m[w]+"\n"+"Indicador 1: "+i[w][0]+"\n"+"Indicador 2: "+i[w][1]+"\n"+"Indicador 3: "+i[w][2]+"\n"+"Indicador 4: "+i[w][3]+"\n"+"Indicador 5: "+i[w][4]+"\n"+"Indicador 6: "+i[w][5]+"\n"+"Indicador 7: "+i[w][6]+"\n"+"Indicador 8: "+i[w][7]+"\n"+"Indicador 9: "+i[w][8]+"\n"+"Indicador 10: "+i[w][9]+"\n"+"Estas seguro que lo quieres borrar?\n1.-SI\n2.-NO");
    					int d=Integer.parseInt(a);
    					if(d==1)
    					{
    						m[w]=(" ");
    						JOptionPane.showMessageDialog(null, "Municipio Borrado");
    						matchIndex=w;break;
    					}
    					else
    						JOptionPane.showMessageDialog(null,"Municipio no borrado.");
     
    				}

  8. #8
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: NullPointerException

    Its pretty clear now that array element you try to access is null and hence it can't call equals method on null object resulting in nullpointerexception. Kindly correct the same.
    Quote Originally Posted by LuisEPB View Post
    Here's the whole method:
    Line 72 is ...

     public static void BorrarMunicipio(String m[], int i[][])
    	{
    		String SearchKey=JOptionPane.showInputDialog(null, "Que municipio deseas borrar?");
    		int matchIndex=-1;
     
     
    			for(int w=0;w<10;w++)
    			{
    				if(m[w].equals(SearchKey))
    				{
    				matchIndex=w;
    					String a=JOptionPane.showInputDialog(null, "Municipio: "+m[w]+"\n"+"Indicador 1: "+i[w][0]+"\n"+"Indicador 2: "+i[w][1]+"\n"+"Indicador 3: "+i[w][2]+"\n"+"Indicador 4: "+i[w][3]+"\n"+"Indicador 5: "+i[w][4]+"\n"+"Indicador 6: "+i[w][5]+"\n"+"Indicador 7: "+i[w][6]+"\n"+"Indicador 8: "+i[w][7]+"\n"+"Indicador 9: "+i[w][8]+"\n"+"Indicador 10: "+i[w][9]+"\n"+"Estas seguro que lo quieres borrar?\n1.-SI\n2.-NO");
    					int d=Integer.parseInt(a);
    					if(d==1)
    					{
    						m[w]=(" ");
    						JOptionPane.showMessageDialog(null, "Municipio Borrado");
    						matchIndex=w;break;
    					}
    					else
    						JOptionPane.showMessageDialog(null,"Municipio no borrado.");
     
    				}

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException

    Found my problem!
    I was declaring my arrays inside the do-while cycle!
    So the info i was entering in my methods wasn't saving in the arrays correctly so since it had nothing to compare to it brought up the error, thank you anyway!

    --- Update ---

    Quote Originally Posted by dineshj83 View Post
    Its pretty clear now that array element you try to access is null and hence it can't call equals method on null object resulting in nullpointerexception. Kindly correct the same.
    Ok beat me to it by a few seconds.. Thanks!

Similar Threads

  1. NullPointerException @@
    By bernard1606 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2013, 09:03 PM
  2. NullPointerException?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2013, 11:46 AM
  3. why am I getting NullPointerException.
    By mia_tech in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 24th, 2012, 11:33 AM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM