HELP!!Store into Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huiling25
    New Member
    • Dec 2006
    • 35

    HELP!!Store into Array

    Code:
    import java.io.*;
    import java.util.*;
    
    public class Test
    {
    	private static String[] pdtNoArray=null;
    	private static String smiles=null;
    	
    	public static void main (String[] args)
    	{
    		try
    		{
    	Scanner inFile = new Scanner(new FileReader("/home/huiling/lopac_smiles.txt"));
    			
    			for(int i=0;i<5;i++)
    			{
    				smiles = inFile.next();
    				pdtNoArray[i] = inFile.next();
    			System.out.println(pdtNoArray);
    					
    			}
    		}
    		catch (FileNotFoundException fe)
    		{
    			System.out.println(fe.toString());
    		}
    	}	
    }
    I don't know why my program keeps on giving me a NullPointer exception. The main purpose of this program is to take the data(pdtNo) from a file and store into an array.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by huiling25
    Code:
    import java.io.*;
    import java.util.*;
     
    public class Test
    {
    	private static String[] pdtNoArray=null;
    	private static String smiles=null;
     
    	public static void main (String[] args)
    	{
    		try
    		{
    	Scanner inFile = new Scanner(new FileReader("/home/huiling/lopac_smiles.txt"));
     
    			for(int i=0;i<5;i++)
    			{
    				smiles = inFile.next();
    				pdtNoArray[i] = inFile.next();
    			System.out.println(pdtNoArray);
     
    			}
    		}
    		catch (FileNotFoundException fe)
    		{
    			System.out.println(fe.toString());
    		}
    	}	
    }
    I don't know why my program keeps on giving me a NullPointer exception. The main purpose of this program is to take the data(pdtNo) from a file and store into an array.
    For starters you have

    Code:
    private static String[] pdtNoArray=null;
    and then never in your code did you initialise the array pdtNoArray

    Comment

    • huiling25
      New Member
      • Dec 2006
      • 35

      #3
      Originally posted by r035198x
      For starters you have

      Code:
      private static String[] pdtNoArray=null;
      and then never in your code did you initialise the array pdtNoArray
      How can I initialise pdtNoArray? Is it like this:
      Code:
      private static String[] pdtNoArray={null};

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by huiling25
        How can I initialise pdtNoArray? Is it like this:
        Code:
        private static String[] pdtNoArray={null};
        You need to know the size of the the array first.

        e.g

        private static String[] pdtNoArray= new String[10];

        Comment

        • huiling25
          New Member
          • Dec 2006
          • 35

          #5
          Originally posted by r035198x
          You need to know the size of the the array first.

          e.g

          private static String[] pdtNoArray= new String[10];
          Thanx for ur help, I solve the problem already. Can i ask u how can I initialise my float array?Is it like this (This will be my last question):
          Code:
          	private static float[] tanimoto_coefficient={0.0f};

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by huiling25
            Thanx for ur help, I solve the problem already. Can i ask u how can I initialise my float array?Is it like this (This will be my last question):
            Code:
            	private static float[] tanimoto_coefficient={0.0f};

            No. Same way you did with the String array. Just specify the size. Since your array is static all the values are immediately initialised to 0.0f.

            Code:
             
            private static float[] tanimoto_coefficient= new float[10]; // for size 10 array

            Comment

            • huiling25
              New Member
              • Dec 2006
              • 35

              #7
              Originally posted by r035198x
              No. Same way you did with the String array. Just specify the size. Since your array is static all the values are immediately initialised to 0.0f.

              Code:
               
              private static float[] tanimoto_coefficient= new float[10]; // for size 10 array
              Ok, thanks alot~

              Comment

              Working...