J Welch J Welch - 7 months ago 14
Java Question

Breakpoint errors in javaeclipse

Good evening everyone. I'm working on a piece of homework and I finally have it nearly complete. Currently, the only thing stopping compilation is a breakpoint error at line 42. Eclipse tells me that it is the variable "list" being uninitialized, however, i cant find where or why this is happening.

The program is for a homework assignment in beginning java. It is designed to import a list of names from a text file called names.txt, then be able to sort through them in an interface, and while the menu is yet to be added, I want to get compiled and make sure its working before I go changing things again.

import java.util.*;
import java.io.*;

public class Name {

private String givenName;
private int[] ranks = new int[11];

public static void main( String[] args ) {

List<Name> list = new ArrayList<Name>();

loadFile();

System.out.println( list.get( 0 ).getPop( 0 ) );
}

private static void loadFile() {
Scanner inputStream = null;
String fileName = "names.txt";

try {
inputStream = new Scanner( new File( fileName ) );
}
catch (FileNotFoundException e) {
System.out.println( "Error opening file named: " + fileName );
System.out.println( "Exiting..." );
}

while ( inputStream.hasNextLine() ) {
String line = inputStream.nextLine();
String[] tokens = new String[0];
String givenName = tokens[0];

int[] numList = new int[tokens.length - 1];

for ( int i = 1; i < tokens.length; i++ ) {
numList[i - 1] = Integer.parseInt( tokens[i].trim() );
}

list.add( new Name( givenName, numList ) );
}
}

// here we get the name for the
public Name(String name, int[] popularityRanks) {
givenName = name;

for ( int i = 0; i < 11; i++ ) {
ranks[i] = popularityRanks[i];
}
}

public String getName() {
return givenName;
}

public int getPop( int decade ) {
if ( decade >= 1 && decade <= 11 ) {
return ranks[decade];
}
else {
return -1;
}
}

public String getHistoLine( int decade ) {
String histoLine = ranks[decade] + ": ";

return histoLine;
}

public String getHistogram() {
String histogram = "";

for ( int i = 0; i < 11; i++ ) {
histogram += ranks[i] + ": " + this.getHistoLine( i ) + "\n";
}

return histogram;
}
}


In addition, I used lists to configure the variables, but now i am deeply regretting it as I feel far more comfortable with just multi-dimensional arrays. As this is homework related, I completely understand if no one wants to help me fix this second part and give me some code to change the lists to arrays.

I'm burnt out and just want it to compile at this point. Any pointers on where to go from here?

Answer

Your list declaration/initialization is in the main and you are trying to access it from loadFile method.

Just move you List<Name> list = new ArrayList<Name>(); as a class variable(put it right above the main) and your code should compile.

Eg:

public class Name {

   private String givenName;
   private int[]  ranks = new int[11];
   static List<Name> list = new ArrayList<Name>();

   public static void main( String[] args ) {

      loadFile();
   ......