By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

HELP!!Store into Array

P: 35
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Test
  5. {
  6.     private static String[] pdtNoArray=null;
  7.     private static String smiles=null;
  8.  
  9.     public static void main (String[] args)
  10.     {
  11.         try
  12.         {
  13.     Scanner inFile = new Scanner(new FileReader("/home/huiling/lopac_smiles.txt"));
  14.  
  15.             for(int i=0;i<5;i++)
  16.             {
  17.                 smiles = inFile.next();
  18.                 pdtNoArray[i] = inFile.next();
  19.             System.out.println(pdtNoArray);
  20.  
  21.             }
  22.         }
  23.         catch (FileNotFoundException fe)
  24.         {
  25.             System.out.println(fe.toString());
  26.         }
  27.     }    
  28. }
  29.  
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.
Jan 3 '07 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Test
  5. {
  6.     private static String[] pdtNoArray=null;
  7.     private static String smiles=null;
  8.  
  9.     public static void main (String[] args)
  10.     {
  11.         try
  12.         {
  13.     Scanner inFile = new Scanner(new FileReader("/home/huiling/lopac_smiles.txt"));
  14.  
  15.             for(int i=0;i<5;i++)
  16.             {
  17.                 smiles = inFile.next();
  18.                 pdtNoArray[i] = inFile.next();
  19.             System.out.println(pdtNoArray);
  20.  
  21.             }
  22.         }
  23.         catch (FileNotFoundException fe)
  24.         {
  25.             System.out.println(fe.toString());
  26.         }
  27.     }    
  28. }
  29.  
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

Expand|Select|Wrap|Line Numbers
  1. private static String[] pdtNoArray=null;
  2.  
and then never in your code did you initialise the array pdtNoArray
Jan 3 '07 #2

P: 35
For starters you have

Expand|Select|Wrap|Line Numbers
  1. private static String[] pdtNoArray=null;
  2.  
and then never in your code did you initialise the array pdtNoArray
How can I initialise pdtNoArray? Is it like this:
Expand|Select|Wrap|Line Numbers
  1. private static String[] pdtNoArray={null};
Jan 3 '07 #3

10K+
P: 13,264
How can I initialise pdtNoArray? Is it like this:
Expand|Select|Wrap|Line Numbers
  1. 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];
Jan 3 '07 #4

P: 35
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):
Expand|Select|Wrap|Line Numbers
  1.     private static float[] tanimoto_coefficient={0.0f}; 
Jan 3 '07 #5

10K+
P: 13,264
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):
Expand|Select|Wrap|Line Numbers
  1.     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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. private static float[] tanimoto_coefficient= new float[10]; // for size 10 array 
  3.  
Jan 3 '07 #6

P: 35
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. private static float[] tanimoto_coefficient= new float[10]; // for size 10 array 
  3.  
Ok, thanks alot~
Jan 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.