473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

HELP!!Store into Array

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
6 2550
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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

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

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.