Hey guys, would like to thank everyne for help on previous post.
the following code is meant to populate a hashmap and hash set respectivly, but when i run my programme its giving me a null pointer in my user interface, could anyone help guide me?

package concordanceReader;

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

public class newTEST {

private static Set<String> byWord;	// maps word with sentence
private static Map<String,Set<String>> bySentence;// maps sentence with text

//loads all the text files into java
private static String[] fileList = { 	"
"C:/Documents and Settings/1.txt",
"C:/Documents and Settings/2.txt",
"C:/Documents and Settings/1.txt",
"C:/Documents and Settings/4.txt",

	
public newTEST()
{
	bySentence = new  HashMap<String,Set<String>>();
	byWord = new HashSet<String>();
		
}
	
public static void populateSet() throws Exception{
		
for (String filename : fileList){

Scanner scanner = new Scanner(new File(filename));

while (scanner.hasNext())		
{
String word = scanner.next();
byWord.add(word);
}
				
	}	
			}
	
public static Map<String, Set<String>> populateMap() throws Exception{
		
for (String filename : fileList){
Scanner scanner = new Scanner(new File(filename));
while (scanner.hasNextLine())	{

		populateSet();
		String sentence = scanner.nextLine();
		bySentence.put(sentence, byWord);
}
		

}	
		return bySentence;
	}
	
}

Recommended Answers

All 6 Replies

Not based on what you posted, no. What user interface? You didn't post any code that indicates how you're calling those methods and the code that you did post wouldn't compile anyway because you have unterminated strings and blocks (and you need to indent consistently instead of just scattering lines all over the place).

this is where the code is used, its basically a text user interface...terrible i know :(

package concordanceReader;


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

public class TUImain {

	private static newTEST newTest;
	private static Scanner keyboard;
	private static PrintWriter pw = new PrintWriter(System.out, true); 

	/**
	 * Constructor

	 * @throws Exception 
	 */
	public TUImain(newTEST newTest) throws Exception {

		this.newTest = newTest;

		// create a Scanner object for obtaining input from keyboard
		keyboard = new Scanner(System.in);
		
		returnWord();
	}


	private void returnWord() throws Exception {


		pw.println("Concordance Reader");

		pw.println("Enter Word");
		String entered = keyboard.nextLine();
		
		

		Map<String, Set<String>> word = newTEST.populateMap();
		

		for(Map.Entry<String,Set<String>> map : word.entrySet()) {
			
			String word = map.getKey();  	
			Set<String> sentence = map.getValue(); 
			
			System.out.println(prize.toString() +  word.toString());
		}
		
		System.out.println("Many Congratulations!!");
	}



	public static void main(String[] args) throws Throwable {


		TUImain tui = new TUImain(newTest);



		for(int i = 0; i < 5; i++) {
			tui.returnWord();
		}

		// Draw prizes
		System.out.println();
		tui.returnWord();
	}
}

Well, you didn't say where you were getting the null pointer, but I'm going to guess that it's on this line

TUImain tui = new TUImain(newTest);

because you never initialize your "newTest" variable before using it there, you just declared it at the class level.

On a side note, give your classes, methods, and variables meaningful names so their purpose is clear when you read them. "newTEST" indicates absolutely nothing about the purpose of that class, "returnWord()" doesn't really return anything so that is pretty misleading, etc. Giving things meaningful names will not only make it easier for others to read your code, but also make it more clear in your own mind as you write it.

(Also, don't ever throw exceptions from main() - what's going to catch them?)

im sorry man, i dont understand, where have i initialized my newTest varibale, like where should i remove it, i tried removing the ones in the paranthesis at line 19 but it gave me an error about hte main method

I am saying that you need to initialize "newTest" before you pass it to the constructor here

TUImain tui = new TUImain(newTest);

.You need to actually create that object before using it

newTest = new newTEST();

(see what I mean with the naming thing?)

Just to add Ezzaral: you have interesting situation:
1. You declare a variable:

private static newTEST newTest;

2. Then you call this:

TUImain tui = new TUImain(newTest);

what means, that you call the constructor and pass this variable into it as parameter.
3. In constructor you do this:

this.newTest = newTest;

what means, that you assign the variable of your class with parameter that was passed to constructor (which is the same variable O_O).

So ... to escape such situation you should or specify a new variable of type newTEST and then pass it to constructor;
or make some changes to constructor (maybe fill by standart values).

About throwing exceptions: for example you have such declaration of method:

private void returnWord() throws Exception

it means, that this method can throw an exception, but it is wouldn't be caught in it. It is like warning: "This method, that can throw an exception of type Exception. If you'll call it, process the catch block for it".
Considering that and that that main method is entry point of the program, think about this:

public static void main(String[] args) throws Throwable
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.