I'm working on a code on compatibility and it seems that after I ask and answer the first question, the program just stops, I have no clue why it doesn't go on to ask the next question, can somebody help my figure this out, thanks, oh here is my code.

This is what it says on the bottom of my code after I run it and answer Y:

----jGRASP exec: java Compatibility


Do you either play or watch football? (Y/N)  Y
Do you like going to the movies?  (Y/N)  Exception in thread "main" java.lang.NullPointerException
at Compatibility.main(Compatibility.java:33)


----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Here is my code:

import java.util.Scanner;



public class Compatibility {
public static void main (String [] args){
Scanner boysScanner = new Scanner(System.in);
//input variables
char reply1;
char reply2;
char reply3;
char reply4;
char reply5;
char reply6;



//display questions


System.out.print("Do you either play or watch football? (Y/N)  ");
reply1 = boysScanner.findInLine(".").charAt(0);



System.out.print("Do you like going to the movies?  (Y/N)  ");
reply2 = boysScanner.findInLine(".").charAt(0);


System.out.print("Do you like education?  (Y/N)  ");
reply3 = boysScanner.findInLine(".").charAt(0);


System.out.print("Do you watch football? (Y/N)  ");
reply4 = boysScanner.findInLine(".").charAt(0);


System.out.print("Do you like watching movies?  (Y/N)  ");
reply5 = boysScanner.findInLine(".").charAt(0);


System.out.print("Do you really like college?  (Y/N)  ");
reply6 = boysScanner.findInLine(".").charAt(0);


//determine by pairing
if (reply1 == reply4 && reply2 == reply5 && reply3 == reply6)    {
System.out.println("You two are great for each other.");
}


if (reply1 != reply4 && reply2 != reply5 && reply4 != reply6)  {
System.out.println("You two better stay away from each other");
}
if (reply1 != reply4 && reply2 == reply5 && reply3 == reply6)    {
System.out.println("You two are somewhat good for each other.");
}


if (reply1 != reply4 && reply2 != reply5 && reply4 == reply6)  {
System.out.println("You two better talk somemore.");
}
if (reply1 == reply4 && reply2 != reply5 && reply3 != reply6)    {
System.out.println("You two better talk somemore.");
}


if (reply1 == reply4 && reply2 == reply5 && reply4 != reply6)  {
System.out.println("You two are somewhat good for each other");
}


if (reply1 == reply4 && reply2 != reply5 && reply4 == reply6)  {
System.out.println("You two are somewhat good for each other");
}
}
}

Recommended Answers

All 11 Replies

your code seems to be working fine i jsut copy and pasted it and compiled it and ran it and there is no problem with it. The one thing you might want to do is to either turn the user input into uppercase or lowercase because 'y' and 'Y' are not the same thing

Do you either play or watch football? (Y/N) Y
Do you like going to the movies? (Y/N) Exception in thread "main" java.lang.NullPointerException
at Compatibility.main(Compatibility.java:33)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Then why does it keep saying this and, how are you able to go to the next question, because it doesn't go to the next question, after you answer, "Do you either play or watch football? It just does nothing after that.
It may not have no errors, but it is basically a dead code because it is not working right and i'm been trying to trouble shoo this for hours.

before I click on the solve button, how were you guys able to get it to work, is it my software jgrasp or my complier, which works, how no clue, plus, I tried to zoom in as much as I could, hard to see but it looks like it was working find?

I does not work when I compile it either.

You will find that the method boysScanner.findInLine(".") is returning NULL when you call it the second time. I don't know if this is because of how our consoles work ( mine is within eclipse ) or if there is some other reason for this. The boysScanner is not waiting for a keybord response it seems before executing the boysScanner.findInLine()

try replacing boysScanner.findInLine(".").charAt(0) with boysScanner.nextLine().charAt(0);

I does not work when I compile it either.

You will find that the method boysScanner.findInLine(".") is returning NULL when you call it the second time. I don't know if this is because of how our consoles work ( mine is within eclipse ) or if there is some other reason for this. The boysScanner is not waiting for a keybord response it seems before executing the boysScanner.findInLine()

hmmmmmm, somehere, but, when you watch that clip it seems to work, do you have a clue to whats going on?

Yes, if you look at the API documentation for the Scanner class it says the following about the find InLine method

Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.

so we have not asked the scanner for the nextLine() and since there are no more occorances of . on the same line it will return a null object.

All you are doing with your patten as well is getting the entire string on that line, therefor you don't really need to use a patten.

So I hope this helps, If you want to use the patten matching you will still need to advance the line with nextLine

Yes, if you look at the API documentation for the Scanner class it says the following about the find InLine method

Attempts to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged. This method may block waiting for input that matches the pattern.

so we have not asked the scanner for the nextLine() and since there are no more occorances of . on the same line it will return a null object.

All you are doing with your patten as well is getting the entire string on that line, therefor you don't really need to use a patten.

So I hope this helps, If you want to use the patten matching you will still need to advance the line with nextLine

yes, it works, but what do you mean by pattern matching, so far, its doing what I told it to do, like if the both say yes, then they like each other blah blah blah, but if I'm missing something, let me know, but hey, thanks for the help, I'll feel better sleeping tonight.

You where using the findInLine(String patten) method. This takes a patten and returns the matching string ( Using regular expressions )

so if i Used for instance findInLine("[YN]") it would ignore every other char in the line except for Y or N and return a string with only the chars Y and N so if the original string was "Hello How Are You, Never Better ?" it would return YN

You where using the findInLine(String patten) method. This takes a patten and returns the matching string ( Using regular expressions )

so if i Used for instance findInLine("[YN]") it would ignore every other char in the line except for Y or N and return a string with only the chars Y and N so if the original string was "Hello How Are You, Never Better ?" it would return YN

ohhhhhhhh, ok, now I know, i'll try to remember that for a future reference, but once again, thanks for help. I'm just so mad that I almost rewrote the whole code using numbers to make things less difficult, but that wouldn't solve the problem, lol, but if I knew that line of code, I would have been done with the code 5 hours ago.

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.