Help! my java program is getting errors!?

Help! my java program is getting errors!?
heres the source code.

/**
* @(#)barbaricsVocab.java
*
*
* @author
* @version 1.00 2009/1/2
*/

public class barbaricsVocab {

public barbaricsVocab() {
}
public static void main(String[] args) {

java.util.Scanner x = new java.util.Scanner(System.in); // console input
String word; //word
String pos; //part of speech
String pron; //pronunciation guide
int a = 0; //keeps while loop running.
String ab;
barbaricsVocab bv = new barbaricsVocab();

if (args.length > 0) {

word = args[0];

if (args.length > 1) {

pos = args[1];
pron= null;

if (args.length == 3) {

pron = args[2];

}

} else { //if the word is the only thing given, query for others.

System.out.println(“Please enter your word’s part of speech\n”);
pos = x.next();
System.out.println(“\n”);
System.out.println(“Please type your word’s pronunciation guide. (if you don’t have the pronunciation guide, type x.)\n”);
pron = x.next();
System.out.println(“\n”);
pron= null;
}

} else {

System.out.println(“Please type your word\n”);
word = x.next();
System.out.println(“\n”);
System.out.println(“Please enter your word’s part of speech\n”);
pos = x.next();
System.out.println(“\n”);
System.out.println(“Please type your word’s pronunciation guide. (if you don’t have the pronunciation guide, type x.)\n”);
pron = x.next();
System.out.println(“\n”);

}

int y = bv.finalOutput(word,pos,pron);

System.out.println(“Thank you for using barbarics vocab! \n”);

}

int finalOutput(String w, String p, String pr) {

int len = w.length();
int start = len – 2;
int end = len;
int dstbegin = 0;
char[] endchars = new char[3];

System.out.print(w+” “+p+” “+”(“);
if(pr.equals(“x”)!=true) {
System.out.print(pr);
}
System.out.println(“)\n”);

System.out.println(” ” ().\n”);

if ((p.equals(“n”))||(p.equals(“n.”))) {

System.out.println(w+” is \n”);
} else if ((p.equals(“adj”))||(p.equals(“adj.”))) {

System.out.println(w+” describes something that is \n”);
} else if ((p.equals(“v”))||(p.equals(“v.”))) {

System.out.println(“to “+w+” is to “);
} else if ((p.equals(“adv”))||(p.equals(“adv.”))) {
w.getChars(start, end,endchars , dstbegin);
if((endchars[0]==’l')&&(endchars[1]==’y')) {
System.out.println(“To do something “+w+” is to do something \n”);
} else {
System.out.println(“To do something in a “+w+” manner is to do something \n”);
}
} else {

System.err.println(“ERROR WITH PART OF SPEECH, CAN’T GENERATE DEFINITION\n”);
}

return 1;

}

}

It’s meant to have 3 command line arguments, and if i enter in 1, it works fine, but if i enter in the part of speech, (second arg) like this:

java barbaricsVocab fain adv.

i get this error:

fain adv. (Exception in thread “main” java.lang.NullPointerException
at barbaricsVocab.finalOutput(barbaricsVocab.java:93)
at barbaricsVocab.main(barbaricsVocab.java:70)

and it works fine when i enter in all three command line arguments like this.

java barbaricsVocab fain adv. feyn.

Best answer:

Piece of cake…. You wrote:

if (args.length > 1) {

pos = args[1];
pron= null;
[...]
so with two args, pron = null. You call finalOutput with pron and when you ref it, it’s null and you get the expected error. Instead, set it:

pron = “”;

Tags: , , , ,

Under Forum

1 Comment for Help! my java program is getting errors!?

  • 1. brice.helman  |  April 29th, 2006 at 2:35 pm

    What’s this line?
    int finalOutput(String w, String p, String pr) {

    Your trying to pass things through an object while declaring an int? This doesn’t make sense. Also starting with this line to then end of the file need to be inside the main or else they need to be in their own sub routine.

Leave a Comment for Help! my java program is getting errors!?

Required

Required, hidden

Trackback this post  |  Subscribe to the comments via RSS Feed


Forum