• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException problems in Main

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));

}

}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to resolve a java.lang.NullPointerException
 
robert swartzers
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic