-7

I have created a function in which I am sending data through array along its index so that particular value should convert into binary, but I don't know whether I am correctly sending and returning array.

my code:

 package GA;

public class test {

    public void chr_intval(){

        double [] array = new double[100] ;
            int i=1;
            array[i] = 4;
            System.out.println(Genes(array,i)); // print the value contain array[1] in binary

        }




    @SuppressWarnings("null")
    public double[] Genes(double[] j,int i){ // get the value from 1st index that is 4 hardcoded
        double [] a1=null;
        int k=9;
        //a1[k]=0;
        double d=j[i];
        while(d>1)
        {
            a1[k--]=d%2;
            d=d/2;

        }



        return a1;// return the value in binary that is 100

    }
    public static void main(String[] args) {
        test a = new test();
        a.chr_intval();
    }

}

at console:

Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Unknown Source)
    at java.io.PrintStream.write(Unknown Source)
    at java.io.PrintStream.print(Unknown Source)
    at java.io.PrintStream.println(Unknown Source)
    at GA.test.chr_intval(test.java:10)
    at GA.test.main(test.java:41)
user3508182
  • 457
  • 1
  • 4
  • 13
  • Does your project have a main method that is calling any of those methods? – Josef E. May 08 '14 at 17:54
  • 3
    `@SuppressWarnings("null")` DANGER DANGER DANGER – Mike G May 08 '14 at 17:54
  • i am calling chr_intval() through where i am calling Genes. – user3508182 May 08 '14 at 17:55
  • 1
    The code as posted makes no sense. NPE is easy to diagnose. Open the test.java file, go to line 41, and what object references you dereference. One of those is null. Once you know which one, figure out why you either didn't initialize it using a constructor or misunderstood its scope. – duffymo May 08 '14 at 17:56

3 Answers3

3

Here is a problem:

@SuppressWarnings("null")

Also, your

private char[] Genes(double[] array, int i) { // TODO Auto-generated method stub return null; }

is returning a null value.

Josef E.
  • 2,179
  • 4
  • 15
  • 30
  • i should write char[] instead of double[] Genes(----)?? – user3508182 May 08 '14 at 17:56
  • Well that depends on what your method to return – Josef E. May 08 '14 at 17:57
  • You're trying to `System.out.println` a null value returned by calling Genes ... in your Genes method, it needs to return some type of char[] that System.out.println can handle – Gary Schreiner May 08 '14 at 18:00
  • 1
    But don't get confused with null exceptions. Sometimes you need to catch them in a try catch statement. However, good programming techniques should eliminate the need for those. (Not all the time though! :P) – Josef E. May 08 '14 at 18:03
  • To add to what @Josef E. said, when the Exception happens within your chr_intval() method, it propagates back up to the main method, which is what your stack trace shows. In your main method, you have no exception handling (try...catch block) to either suppress the exception and log the error or handle it in some other way. Learning good exception handling early will help you in the future when you have larger projects. – Gary Schreiner May 08 '14 at 18:07
0

Genes1() is never executed, you are returning null from the Gene() method.

frederick_c_siu
  • 540
  • 3
  • 8
0

This is happening because your Genes method is returning null. The println() function doesn't know how to handle a null value, and thus throws a NullPointerException.

Just edit your function to return something other than null.

Gliptal
  • 350
  • 1
  • 10
  • that is the problemi am not picking up i event wrote return a1[k]; but does not know??? – user3508182 May 08 '14 at 18:00
  • I think you are confusing Genes() with Genes1(). – Gliptal May 08 '14 at 18:05
  • as i have told in explanation i am converting decimal to binary and my logic is send to gene(...) . – user3508182 May 08 '14 at 18:17
  • I can't really understand your code. Why is `k = 9`? You are also creating `array` with dim 100, but never fill it before passing it to `genes`. – Gliptal May 08 '14 at 18:28
  • this is for storing binary digits. like when 4%2=0. 0 willbe place in 8th index and so on. – user3508182 May 08 '14 at 18:30
  • actually i am coming from c++ and have no idea of such things. Rather telling politely all focus on giving anti points. haaah as all know i have written i am noob so could ask question or not. – user3508182 May 08 '14 at 18:32
  • Try changing your code so that it's more readable: as it is now understanding is way too complicated. Change variable and method names to be more clear. – Gliptal May 08 '14 at 18:40
  • binary ranges : 2 bit 4 bit 8bit and so on. having intialising with 9 would turn into 8 when a1[k--], which means in 8 th index place the value e.g 4%2 gives 0 and zero will be place in 8th index: |1st index|.........|1 in 6th index||0 in 7th index||0 in 8th index| forming 00000100 = 4 – user3508182 May 08 '14 at 18:43