Results 1 to 4 of 4
  1. #1
    pankaj80387 is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default how to remove NullPointerException

    Java Code:
    import java.io.*;
    public class crypt2{
     
    int output1[][];
    int output2[][];
    public  void transform(int [][]x,int y)
    {
     for(int i=0;i<y;i++)
     {
       for(int j=0;j<y;j++)
       {
        if(x[i][j]==0)
        { 
        int m=(int)(Math.random()*2);
        if(m==0)
        {
        output1[i][j]=0;
        output2[i][j]=1;
        }
    	else
     	{
    	output1[i][j]=1;
    	output2[i][j]=0;
    	}
        }
          else
          {
          int n=(int)(Math.random()*2);
          if(n==0)
          {
          output1[i][j]=0;
          output2[i][j]=0;
          }
    	else
     	{
    	output1[i][j]=1;
    	output2[i][j]=1;
            }
          } 
       }
     }
    }
    
    
    
    public static void main(String args[]){
     crypt2 c=new crypt2();
     //int input1;
     int m;
     try{
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("enter the dimensions for array");
     m=Integer.parseInt(br.readLine());
     int [][]input=new int[m][m];
      for(int i=0;i<m;i++)
      {
       for(int j=0;j<m;j++)
       {
        System.out.println("enter [" +i+ "] [" +j+ "] th element");
        input[i][j]=Integer.parseInt(br.readLine());
       }
      }
      //call to method transform
      c.transform(input,m);
     
      for(int i=0;i<m;i++)
      {
       for(int j=0;j<m;j++)
       {
        System.out.print(input[i][j]);
       } 
       System.out.println();
      }
      System.out.println("-------------------------");
       for(int i=0;i<m;i++)
       {
        for(int j=0;j<m;j++)
        {
         System.out.print(c.output1[i][j]);
        } 
        System.out.println();
       }
       System.out.println("-------------------------");
    	for(int i=0;i<m;i++)
    	 {
    	  for(int j=0;j<m;j++)
     	  {
    	   System.out.print(c.output2[i][j]);
     	  } 
     	 System.out.println();
    	 }
     }
     catch(Exception e)
     {
      System.out.println("exception is " +e);
      e.printStackTrace();
     }
    }
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 05-16-2010 at 04:31 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome. I've added code tags to your post so that it will be more easily readable. To learn how to do this yourself for your next post, please read the link on code tags in my signature.

    Also, please tell us which line causes the NPE to be thrown. This is often all the clue you need to solve this problem: find the line, check the variables used on that line, and one of these objects has not been initialized.

    Much luck and again welcome!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Edit: I've run your code and my advice above is correct. There's only one object used on the line that causes the NPE, and if you inspect your code you'll see that it was never initialized. Initialize the arrays before you use them and your NPE will go away.

    Much luck.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. How to remove rectanlge box.
    By kgkamaraj in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 09:20 AM
  2. Remove headline
    By amarelv in forum New To Java
    Replies: 4
    Last Post: 11-07-2008, 05:36 AM
  3. how to remove this error
    By ravinder64 in forum Java Servlet
    Replies: 1
    Last Post: 11-01-2008, 08:04 AM
  4. how to remove an old version of JDK
    By tommy in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 09:59 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
  •