Results 1 to 6 of 6
  1. #1
    maxspyderweb is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default NullPointerException

    Hi to everyone, I want to start telling you sorry about my english..

    Here is the java class

    Java Code:
    package utility;
    
    
    public class Decodificatore {
    
    	public static NBit[] decode(String bits, int n, int freq){
    		NBit.len = n;
    		int nnbit =bits.length()/n;
    		int subnnbit = nnbit/freq;
    		System.out.println(nnbit+""+subnnbit);
    		
    		NBit[] decoded = new NBit[subnnbit];
    		for(int i=0; i<subnnbit;i++){
    			NBit[] semidec = new NBit[freq];
    				for(int j=0; j<freq;j++){
    					String s = bits.substring(i*freq+j, i*freq+j+n);
    					semidec[j].set(s);
    					
    				}
    			decoded[i]= ConfrBitFreq.Confronta(semidec);
    		}
    		return  decoded;
    	}
    }
    the error is

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at utility.Decodificatore.decode(Decodificatore.java:17)
    	at Input.main(Input.java:13)

    and the NBit class is

    Java Code:
    package utility;
    
    import java.util.BitSet;
    
    public class NBit {
    	
    	public static int len = 0;
    	
    	public BitSet nBit = new BitSet(len);
    	
    	public void set(String s)
    	{
    		for(int i=0; i < len ; i++)
    		{
    			String sts = s.substring(i,i+1);
    			if(sts == "0"){
    				nBit.set(i, i+1, false);
    			} else nBit.set(i,i+1,true);
    		}
    	}
    	
    	public static boolean equal(NBit a, NBit b){
    		boolean risultato = false;
    		for(int i=0; i< len; i++){
    			if(a.nBit.get(i)==b.nBit.get(i)){
    				risultato = true;
    			} else {risultato = false; continue;}
    		}
    		return risultato;
    	}
    
    
    	public static String NBitoString(NBit[] bites)
    	{
    		String finale = "";
    		for(int j=0; j < bites.length;j++){
    		
    			String s="";
    		
    		for(int i=0; i<len;i++){
    			if(bites[j].nBit.get(i)){
    				s=s+"1";
    			} else s=s+"0";
    		}
    		finale = finale+s;
    		}
    		return finale;
    	}
    
    	
    }
    sorry I'm totally new to java, as you could see this is quite chaotic but I think the problem is something very simple, please help me! xD

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    		NBit[] semidec = new NBit[freq];
    				for(int j=0; j<freq;j++){
    					String s = bits.substring(i*freq+j, i*freq+j+n);
    					semidec[j].set(s);
    With NBit[] semidec = new NBit[freq]; you are creating an array with the size of freq, but each item in the array (0-freq-1) is initialized with null! So on the last line you call null.set(s) --> NPE
    So you have to create NBit objects first.

  3. #3
    maxspyderweb is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    thank you but how can I inizialize an array of objects
    I have instancied the object first and then I've overwritten all the null with the method set(String) but yeah semidec[i] is null ok I've added a constructor in the class NBit

    Java Code:
    	public NBit(){
    
    		nBit.set(0, len, false);
    	}
    but there are no call like new NBit() so it doesn't work.. I dunno what do to..

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    Before doing this:
    Java Code:
    semidec[j].set(s);
    You want to create a new NBit() and assign that to semidec[i].

  5. #5
    maxspyderweb is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    ok done, I've done a for cycle that creates for every NBit[] xxx = new NBit();

    I think this is not an efficient method, I would like to create a Type that rappresent an n number of bits (nbits) I've created an object any suggestions?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    Since you're looping around the array anyway why is it inefficient?

Similar Threads

  1. NullPointerException
    By GPB in forum New To Java
    Replies: 8
    Last Post: 02-21-2010, 03:05 PM
  2. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 05:43 PM
  3. NullPointerException help?
    By fab5freddy in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 08:26 PM
  4. NullPointerException help me!
    By phancuong87 in forum New To Java
    Replies: 4
    Last Post: 01-19-2010, 04:01 PM
  5. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 08:42 AM

Posting Permissions

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