Results 1 to 6 of 6
Thread: NullPointerException
- 03-01-2011, 05:22 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
NullPointerException
Hi to everyone, I want to start telling you sorry about my english..
Here is the java class
the error isJava 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; } }
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
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! xDJava 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; } }
- 03-01-2011, 05:36 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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) --> NPEJava 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);
So you have to create NBit objects first.
- 03-01-2011, 05:53 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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
but there are no call like new NBit() so it doesn't work.. I dunno what do to..Java Code:public NBit(){ nBit.set(0, len, false); }
- 03-02-2011, 09:41 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Before doing this:
You want to create a new NBit() and assign that to semidec[i].Java Code:semidec[j].set(s);
- 03-02-2011, 09:45 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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?
- 03-02-2011, 10:42 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
NullPointerException
By GPB in forum New To JavaReplies: 8Last Post: 02-21-2010, 03:05 PM -
NullPointerException
By Juuno in forum New To JavaReplies: 1Last Post: 02-11-2010, 05:43 PM -
NullPointerException help?
By fab5freddy in forum New To JavaReplies: 2Last Post: 02-04-2010, 08:26 PM -
NullPointerException help me!
By phancuong87 in forum New To JavaReplies: 4Last Post: 01-19-2010, 04:01 PM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks