5 Replies - 1310 Views - Last Post: 14 September 2011 - 07:30 AM Rate Topic: -----

#1 HelloWorldLMAO  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Prompt user for input file

Posted 14 September 2011 - 06:23 AM

having issues with propmting user for the input file and reading the file to run method.

errors:

run:
Enter filename: H:/cosc237-lab1-input.txt

Exception in thread "main" java.lang.NullPointerException
at lab1.LAB1.findMode(LAB1.java:36)
at lab1.LAB1.Mode(LAB1.java:28)
at lab1.LAB1.main(LAB1.java:23)
Java Result: 1

package lab1;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;


public class LAB1 {
    private static int[] arr;
    
    public static void main(String[] args) throws FileNotFoundException {
        
   Scanner in = new Scanner(System.in);
   System.out.print("Enter filename: ");
   String filename = in.nextLine();
        
        
        // Frequencies of all values in file (-9...9)
        int[] frequencies = new int[19];
  
        Mode(in, frequencies);

 
    }
 static void Mode(Scanner in, int[] modefreq)  {
 int mode=findMode(arr);
 System.out.println(mode);
 }

 public static int findMode(int[] arr){
 int mode=-1;
 int modefreq=-1;

 for (int i=0;i<arr.length;i++){
 int current=arr[i];
 int freq=howMany(arr,current);
 if (freq>modefreq){
 mode=current;
 modefreq=freq;
 }
 }
 return(mode);
 }

 public static int howMany(int[] arr,int c){
 int howm=0;
 for (int i=0;i<arr.length;i++){
 if (arr[i]==c) howm++;
 }
 return (howm);
 }


    }


Is This A Good Question/Topic? 0
  • +

Replies To: Prompt user for input file

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Prompt user for input file

Posted 14 September 2011 - 06:45 AM

You have a static variable named arr which you never initialize. This variable is thus null.

In line 28 you pass that variable (which is null) as an argument to findMode. findMode then tries to access its length field in line 36. Since arr is null, that causes a NullPointerException.

You probably meant to pass frequencies in line 28, not arr. Honestly I'm not sure why arr exists at all in your code.
Was This Post Helpful? 0
  • +
  • -

#3 HelloWorldLMAO  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: Prompt user for input file

Posted 14 September 2011 - 06:53 AM

i got lost trying to fix it and came out with this. the error returned was.

error:
run:
Enter filename: H:/cosc237-lab1-input.txt

Exception in thread "main" java.lang.NullPointerException
at lab1.LAB1.findMode(LAB1.java:36)
at lab1.LAB1.Mode(LAB1.java:28)
at lab1.LAB1.main(LAB1.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 minutes 32 seconds)


package lab1;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;


public class LAB1 {
    private static int[] arr;
    
    public static void main(String[] args) throws FileNotFoundException {
        

        // Frequencies of all values in file (-9...9)
        int[] freq = new int[19];
        
           Scanner in = new Scanner(System.in);
           System.out.print("Enter filename: ");
           String filename = in.nextLine();
           Mode(in, freq);

        
 
    }
 private static void Mode(Scanner in, int[] freq)  {
 int mode=findMode(freq);
 System.out.println(mode);
 }

 public static int findMode(int[] freq){
 int mode=-1;
 int modefreq=-1;

 for (int i=0;i<freq.length;i++){
 int current=freq[i];
 int freq=howMany(mode,current);
 if (freq>modefreq){
 mode=current;
 modefreq=freq;
 }
 }
 return(mode);
 }

 public static int howMany(int[] freq,int c){
 int howm=0;
 for (int i=0;i<freq.length;i++){
 if (freq[i]==c) howm++;
 }
 return (howm);
 }

Was This Post Helpful? 0
  • +
  • -

#4 HelloWorldLMAO  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: Prompt user for input file

Posted 14 September 2011 - 07:08 AM

oops i meant
package lab1;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;


public class LAB1 {
    private static int[] frequencies;
    
    public static void main(String[] args) throws FileNotFoundException {
        
   Scanner in = new Scanner(System.in);
   System.out.print("Enter filename: ");
   String filename = in.nextLine();
        
        
        // Frequencies of all values in file (-9...9)
        int[] frequencies = new int[19];
  
        Mode(in, frequencies);

 
    }
 static void Mode(Scanner in, int[] modefreq)  {
 int mode=findMode(frequencies);
 System.out.println(mode);
 }

 public static int findMode(int[] frequencies){
 int mode=-1;
 int modefreq=-1;

 for (int i=0;i<frequencies.length;i++){
 int current=frequencies[i];
 int freq=howMany(frequencies,current);
 if (freq>modefreq){
 mode=current;
 modefreq=freq;
 }
 }
 return(mode);
 }

 public static int howMany(int[] frequencies,int c){
 int howm=0;
 for (int i=0;i<frequencies.length;i++){
 if (frequencies[i]==c) howm++;
 }
 return (howm);
 }


    }

Was This Post Helpful? 0
  • +
  • -

#5 cmpshr  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 120
  • Joined: 22-August 11

Re: Prompt user for input file

Posted 14 September 2011 - 07:25 AM

public class LAB1 {
    private static int[] frequencies;  // this is an array visible to all method
    
    public static void main(String[] args) throws FileNotFoundException {
        
      Scanner in = new Scanner(System.in);
      System.out.print("Enter filename: ");
      String filename = in.nextLine();
        
        
        // this declare an array of int[] that will be only visible in this method
        int[] frequencies = new int[19];
  
        Mode(in, frequencies);

    }
    static void Mode(Scanner in, int[] modefreq)  {  // this reference the array frequencies defined in main()
      int mode=findMode(frequencies);  // this references the frequencies array declared at the top of the class
      System.out.println(mode);
    }

    public static int findMode(int[] frequencies){
//      here frequencies is the one at the top of the class, not initialized no where yet
      int mode=-1;
      int modefreq=-1;

      for (int i=0;i<frequencies.length;i++){
         int current=frequencies[i];
         int freq=howMany(frequencies,current);
         if (freq>modefreq){
              mode=current;


Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Prompt user for input file

Posted 14 September 2011 - 07:30 AM

First of all, I apologize. I meant to write modefreq, when I wrote frequencies. I.e. I suggested you probably intended to call findMode withe Mode's second argument as its argument, not the static variable that you never initialized.

That being said: I'd really urge you to think about why the static variable exists at all and whether you need it. Because you almost certainly don't (and if you do, you should probably initialize it at some point). The fact that you thought that renaming the static variable (which I told you you never initialize) would solve your problem, makes me think that you don't really understand your own code, which is not an ideal situation. You should probably work on that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1