Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: More Array Troubles

    Quote Originally Posted by Norm View Post
    Have you tried it yet? You can change it back if it doesn't compile.

    In the main() method:
    Use the Calculations object to call the ReadFiles methods.
    Do NOT create a ReadFiles object.
    It is working. Thank you thank you thank you.

  2. #22
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Switching from String to Float

    I have run into a problem (again) that I cannot figure out. I am trying to create an array that is declared float by inputting information from another array that is string, but I keep getting the same compile error.
    This is the error:
    java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at java.lang.Float.parseFloat(Unknown Source)
    at Calculations.createDataArray(Calculations.java:40)
    at DataAnalyzer.main(DataAnalyzer.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
    >

    This is the short section of code that I am working with.
    Java Code:
      public void createDataArray()
      {
        try
        {
          System.out.println("Your file is being read to organize information for processing. This may take several minutes.");
          for (int x = 0; x <= 431; x++)
          {
            for (int y = 1; y <= 7; y++)
            {
              dataArray[x][y] = Float.parseFloat(myFBfile[x][y]);
              //this is a test to make sure the array is working properly
              System.out.println (dataArray[x][y]);
            }
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
            
      }
    Any help is greatly appreciated.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Switching from String to Float

    java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at java.lang.Float.parseFloat(Unknown Source)
    at Calculations.createDataArray(Calculations.java:40)
    That is an execution time error, not a compiler error.
    The code on line 40 used a null value in its call to the parseFloat() method. Check why the value passed to parseFloat() is null and change the code to prevent that from happening.
    Either test the contents of the variable before passing it to parseFloat()
    or make sure the contents of the variable is not null.
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Switching from String to Float

    Quote Originally Posted by Norm View Post
    or make sure the contents of the variable is not null.
    That is probably the more interesting thing to investigate. How is that myFBfile[][] array created?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #25
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Switching from String to Float

    Ok, so this is really bizarre. When I run the file through the program it shows the first two sets of values and the last three sets of values as null. It is really odd as there is nothing at the beginning or end that would indicate a null field. I tried uploading the file to show but it will not let me upload a .csv

  6. #26
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Switching from String to Float

    Did you read the other's replies? Where is myFBfile[][] being initialized with values?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #27
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Switching from String to Float

    I will post the entire code
    Java Code:
    /**
     * DataAnalyzer Class
     * This class instantiates the methods from the ReadFiles Class and Calculations Class.
     * @author Brad Strassburger
     * @version 1 March 20, 2014
     */
    public class DataAnalyzer
    {
      public static void main (String[] args)
      {
        //This creates an instance of ReadFiles
        ReadFiles aReadFiles = new ReadFiles();
        Calculations aCalculations = new Calculations();
        
        //This calls the method fileCheck in the ReadFiles Class
        aReadFiles.fileCheck();
        
        //This calls the method findRows in the ReadFiles Class
        aReadFiles.findRows();
        
        //This calls the method printArray in the ReadFiles Class
        //aReadFiles.printArray();
        
        //This calls the method convertFile in the ReadFiles Class through the Calculations Class
        aCalculations.convertFile();
    
        //This calls the method createDateArray in the Calculations Class    
        aCalculations.createDateArray();
        
        //This calls the method createDataArray in the Calculations Class
        aCalculations.createDataArray();
        
      }
    }
    Java Code:
    import java.nio.file.*;
    import static java.nio.file.AccessMode.*;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.StringTokenizer;
    
    /**
     * ReadFiles Class
     * This class executes all of the methods called bt DataAnalyzer.
     * @author Brad Strassburger
     * @version 1 March 20, 2014
     */
    
    public class ReadFiles
    {
      public static int numberOfRows;
      public static int rowNumber = 0;
      public static int columnNumber = 0;
      public static int newRowNumber = 0;
      public static int newColumnNumber = 0;
      public String [][] myFBfile = new String [430][8];
    
      //This method attempts to read a file to ensure that it is available to be used by the rest of the program.
      public void fileCheck()
      {
        Path filePath = Paths.get("C:\\Users\\Brad\\Downloads\\FB.csv");
        System.out.println("Path is " + filePath.toString());
        
        try
        {
          filePath.getFileSystem().provider().checkAccess(filePath, READ, EXECUTE);
          System.out.println("File can be read and executed");
        }
        
        catch (IOException e)
        {
          System.out.println ("File cannot be used for this application");
        }
      }
      
      //This method reads a file, then tokenizes it to find the number of rows in the file.
      public void findRows()
      {
        String filePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(filePath));
          StringTokenizer st = null;
          System.out.println("Your file is being analysed. This may take several minutes.");
    
          while((filePath = br.readLine()) != null)
          {
            rowNumber++;
                    
            st = new StringTokenizer(filePath, ",");
            columnNumber = 0;
          }
        }
        
        catch (FileNotFoundException e)
        {
          System.out.println ("Number of rows for this file cannot be found");
        } 
        
        catch (IOException e) 
        {
          System.out.println ("Number of rows for this file cannot be found");
        }
            
        System.out.println("Total Rows: " + (rowNumber));
      }
      
      //This method converts the file into an array to be used by the rest of the program.
      public void convertFile()
      {
        String convertFilePath = ("C:\\Users\\Brad\\Downloads\\FB.csv");
        //The fields for the file are: Date,Open,High,Low,Close,Volume,Adj Close
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(convertFilePath));
          StringTokenizer st = null;
          System.out.println("Your file is being converted. This may take several minutes.");
    
          while((convertFilePath = br.readLine()) != null)
          {
    
            
            st = new StringTokenizer(convertFilePath, ",");
    
            while(st.hasMoreTokens())
            {
              myFBfile[newRowNumber][newColumnNumber] = st.nextToken();
              newColumnNumber++;          
              //this is a test to make sure the array is working properly
              //System.out.println (newRowNumber +" "+ newColumnNumber +" "+ myFBfile[newRowNumber][newColumnNumber]);
            }
            newRowNumber++;
            newColumnNumber = 0;
          }
        }       
        catch (FileNotFoundException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        } 
        catch (IOException e) 
        {
          System.out.println("Encountered critical error. Your file has not been converted");
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
        
      }
      
      //This method prints out all of the data from the array
      public void printArray()
      {
        for (int x = 0; x <= rowNumber; x++)
        {
          for(int y = 0; y <= 7; y++)
          {
            System.out.print (x +" "+ y +" "+ myFBfile[x][y]);
          }
          //int y = 0;
        }
      }
      
      //This method returns the array
      public void returnArray()
      {
        //return myFBfile;
      }
    }
    Java Code:
    public class Calculations extends ReadFiles
    {
      public String []dateArray = new String [myFBfile.length];
      public float [][]dataArray = new float[myFBfile.length][7];
    
     /**
     * Calculations Class
     * This class executes all of the methods called bt DataAnalyzer.
     * @author Brad Strassburger
     * @version 1 March 20, 2014
     */
      public void createDateArray()
      {     
        try
        {
          System.out.println("Your file is being read to find dates. This may take several minutes.");
          for (int x = 0; x <= myFBfile.length; x++)
          {
            dateArray[x] = myFBfile[x][0];
            //this is a test to make sure the array is working properly
            //System.out.println (dateArray[x]);
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
      }
      
      public void createDataArray()
      {
        try
        {
          System.out.println("Your file is being read to organize information for processing. This may take several minutes.");
          for (int x = 0; x <= 431; x++)
          {
            for (int y = 1; y <= 7; y++)
            {
              dataArray[x][y] = Float.parseFloat(myFBfile[x][y]);
              //this is a test to make sure the array is working properly
              System.out.println (dataArray[x][y]);
            }
          }
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
          System.out.println ("Your file exceeds the available size limit.");
        }
            
      }
     
      public void printArray()
      {
       
      }
     
      public void performCalculations()
      {
       
      }
      
    }

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Switching from String to Float

    Why does the Calculations class extend the ReadFiles class?
    What is the purpose of creating a separate instance of the ReadFiles class?

    Where in the instance of the Calculations class are any values put into the myFBfile array?


    Is this the same topic: More Array Troubles

    The problem was answered several times in that thread.
    Last edited by Norm; 03-25-2014 at 09:07 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Switching from String to Float

    And there's the problem. You have an instance of ReadFiles and an instance of Calculations
    (which extends ReadFiles). You only initialize the matrix in the first instance but try to use it in
    the second instance. Those are two separate instances. So the matrix in the second instance
    has not been initialized yet.

    Edit: Strange. I didn't even notice the other related comments. Looks like it has already
    been covered.

    Regards,
    Jim
    Last edited by jim829; 03-25-2014 at 09:11 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: More Array Troubles

    Threads merged.
    If you don't understand my response, don't ignore it, ask a question.

  11. #31
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: More Array Troubles

    Thanks Norm. I thought I was in the Twilight zone or something. Glad to know I haven't totally lost it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #32
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: More Array Troubles

    Quote Originally Posted by jim829 View Post
    Thanks Norm. I thought I was in the Twilight zone or something. Glad to know I haven't totally lost it.

    Regards,
    Jim
    Who is this "Norm" you speak of? There is nobody in this thread by that name.

    *tududududu tudududuuuuu*
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Array Troubles
    By shodai in forum New To Java
    Replies: 28
    Last Post: 02-16-2014, 11:15 PM
  2. Array Troubles
    By shodai in forum New To Java
    Replies: 2
    Last Post: 02-11-2014, 05:43 PM
  3. Troubles with Vector
    By Outsider418 in forum AWT / Swing
    Replies: 8
    Last Post: 05-21-2012, 12:10 PM
  4. Textpad troubles
    By Sthassaris in forum Other IDEs
    Replies: 3
    Last Post: 06-03-2011, 09:38 PM
  5. [SOLVED] Array troubles, yes I searched...
    By Reiyn in forum New To Java
    Replies: 11
    Last Post: 04-16-2009, 11:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •