Results 1 to 3 of 3
  1. #1
    ivanblackz is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default NullPointerException for String array

    I am making a PriorityQueue class for a small program, but it keeps giving(when i call on the push function) me a nullpointer exception in line 13:
    else if(hi==a.length)
    .
    I do not understand why, It'd be great if anyone here could help me out.

    Java Code:
    public class PriorityQ
    {
    	private String[] a;
    	private int lo,hi;
    	private boolean isSort;
    	
    	public PriorityQ()
    	{String[] a=new String[1024];lo=0;hi=0;isSort=false;}
    	
    	public void push(String s)
    	{
    		if(lo>0){a[--lo]=s;}
    		else if(hi==a.length)
    		{this.resize(2); a[hi]=s;}
    		else
    		{a[hi]=s;}
    		isSort=false;
    	}
    ...
    Last edited by ivanblackz; 02-15-2013 at 07:57 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,015
    Rep Power
    10

    Default Re: NullPointerException for String array

    In your constructor, you're declaring a new String array with the same name as the class variable String array.

    That is some strange formatting, by the way. Try this to get a better idea of what you're doing:
    Java Code:
    public PriorityQ(){
       String[] a=new String[1024];
       System.out.println(a);
       System.out.println(this.a);
       lo=0;
       hi=0;
       isSort=false;
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ivanblackz is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: NullPointerException for String array

    I see, thank you. That means i just have to replace "String[] a=new String[1024];" with "a=new String[1024];", and now it works ^^,
    This error had been troubling my mind for a while.
    Thanks a lot.

Similar Threads

  1. NullPointerException when there is data in array
    By anoorally in forum New To Java
    Replies: 3
    Last Post: 02-09-2012, 12:33 AM
  2. NullPointerException when accessing array
    By aianta in forum New To Java
    Replies: 3
    Last Post: 10-15-2011, 11:19 PM
  3. text into array...nullpointerexception
    By csuever in forum New To Java
    Replies: 4
    Last Post: 04-01-2011, 01:08 AM
  4. Replies: 8
    Last Post: 03-31-2011, 07:09 AM
  5. NullPointerException converting String to double
    By infaddict in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 07:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •