aspose file tools*
The moose likes Beginning Java and the fly likes NullPointerException problems in Main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException problems in Main" Watch "NullPointerException problems in Main" New topic
Author

NullPointerException problems in Main

robert swartzers
Greenhorn

Joined: Nov 18, 2005
Posts: 2
I am an Undergarduate student who is trying to get classes to work with association. All of the classes below compile and Main will run, but then falls over with a NullPointerException (which is commented in main)
I have been trying to find out what is going wrong for two days and getting no where.
------------------------------------------------
import java.io.*;
import java.util.*;

public class Codex
{
Dictionary myDictionary;

Codex(Dictionary aDictionary)
{
myDictionary = aDictionary;
myDictionary.setCodex(this);
}

//Numbers method, to convert words into numbers
public String CodexConvert(String word)
{
StringBuffer nos = new StringBuffer("");
char c; //c is the character in the word
char n; //the character in the output string, ie numbers
//loop through while word untill all letters are converted

for (int i=0; i<word.length(); i++)
{
c = word.charAt(i);
switch (c)
{
case 'a':
case 'b':
case 'c':
n='2';

break;

case 'd':

case 'e':

case 'f':

n='3';

break;

case 'g':

case 'h':

case 'i':

n='4';

break;

case 'j':

case 'k':

case 'l':

n='5';

break;

case 'm':

case 'n':

case 'o':

n='6';

break;

case 'p':

case 'q':

case 'r':

case 's':

n='7';

break;

case 't':

case 'u':

case 'v':

n='8';

break;

case 'w':

case 'x':

case 'y':

case 'z':

n='9';

break;

default:

n='0';

break;

}

nos.append(n);
}
return new String(nos);

}

}
-------------------------------------------------
import java.io.*;

import java.util.*;



public class Dictionary

{
public HashMap map;
private String wordnos, word;
Codex myCodex;

public Dictionary()
{
}
//dictionary method
void Dictionary()
throws FileNotFoundException, IOException
{
//Read in the dictionary word file
BufferedReader readDictionary = new BufferedReader(new FileReader("MyWords"));

//store each word, by line in variable
word = readDictionary.readLine();

while (word != null)
{
wordnos = myCodex.CodexConvert(word); //converts words into numbers + stores them in wordnos
//add wordnos to map, if it does not exist
if (!map.containsKey(wordnos))
{
map.put(wordnos, new Vector()); //create new vector for wordnos
} //add words to words to match wordnos vectors

((Vector)map.get(wordnos)).add(word);
word = readDictionary.readLine();
}
readDictionary.close();

}
void setCodex(Codex aCodex)
{
myCodex = aCodex;
}
}

------------------------------------------------
//Main section to test the dictionary and Codex
import java.io.*;
import java.util.*;
public class DictTest
{
public static void main(String[] args)
{
String numbersIn; //variable for users input
//recieve and store users input
System.out.println("Enter your numbers here : ");
System.out.println();

//read in number string and store in numbersIn variable
numbersIn= KBInput.readString();

//create object MyDictionary and print out results
Dictionary RatsDictionary = new Dictionary();
Codex RatsCodex = new Codex(RatsDictionary);
System.out.println();

//Error here : Exception in thread "main" java.lang.NullPointerException
System.out.println("Words that match your number input are:" +RatsDictionary.map.get(numbersIn));

}

}
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

RatsDictionary.map is null.

In your code, you're not initialising map in class Dictionary anywhere, so it will be null. Try this in your class Dictionary

HashMap map = new HashMap();


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jody Brown
Ranch Hand

Joined: Nov 09, 2005
Posts: 43
At a glance, I can't see where your map object is being instantiated inside your Dictionary class. When you try to use map, I think you will get a null pointer exception. On a side note, in your main method of your test class, you have a variable that has its first letter capitalised (RatsDictionary). This syntax is confusing because this is the format used in class names. The first thing I tried to look for when trying to debug your code by eye was for a the class called RatsDictionary which didnt exist. Just something to bear in mind. Now I have posted this, I will actually debug it and see if I was right.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

How to resolve a java.lang.NullPointerException
robert swartzers
Greenhorn

Joined: Nov 18, 2005
Posts: 2
The missing HashMap map = new HashMap(); was a typo on my part when I posted the code. I have changed the uppercase name of the instatiated objects now.
 
 
subject: NullPointerException problems in Main