0

I am trying to create a code in Java, which is going to read two words from anagram.txt file, and see if those two words are anagrams. I am getting an error java.lang.NullPointerException. I am not sure what am I doing wrong, so any suggestions? I guess that o2.Anagram() needs to have some variables, but I want it to call Strings from txt file... I am a beginner, so don't laugh at me :) Thank you!

This is a class that reads from txt file:

import java.io.File;
import java.util.Scanner;

public class Klasa {

 public Scanner x;
 String s1;
 String s2;

 public void openFile(){
     try{
     x = new Scanner(new File("C:\\anagram.txt"));
     }
     catch(Exception e){
         System.out.println("Error");
     }
 }

 public void readFile(){
     while (x.hasNext()){
         String s1 = x.next();
         String s2 = x.next();           
     }
 }

 public void closeFile(){
     x.close();
 }   
}

This is a class that should compare two words from anagram.txt and see if they are anagrams:

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

public class isAnagram extends Klasa{

public void Anagram(){

     boolean status = true;

     if(s1.length() != s2.length()){
         status = false;
     }
     else{
         char[] s1Array = s1.toLowerCase().toCharArray();
         char[] s2Array = s2.toLowerCase().toCharArray();

         Arrays.sort(s1Array);
         Arrays.sort(s2Array);

         status = Arrays.equals(s1Array, s2Array);
     }           
           //Output          
            if(status)
            {
                System.out.println(s1+" and "+s2+" are anagrams");
            }
            else
            {
                System.out.println(s1+" and "+s2+" are not anagrams");
            }
     }
 }

And this is main class:

    public class mainClass{

     public static void main(String[] args){            

        Klasa o1 = new Klasa();     
        o1.openFile();
        o1.readFile();      
        o1.closeFile();

        isAnagram o2 = new isAnagram();         
        o2.Anagram();        
    }       
  }
TimeToCode
  • 901
  • 2
  • 16
  • 34
  • Can you post stacktrace seems like s1 or s2 – HRgiger Sep 20 '16 at 14:19
  • In method `readFile()` you create local variabels `s1, s2`, which hide the globale ones which you use later in your code. Theat's why you get a NPE. Remove the `String` infront of them and try again. – SubOptimal Sep 20 '16 at 14:19
  • 1
    Besides that, there is no connection between the data that you read in the object `o1` and the object `o2`. The values of the variables won't magically go from one object to the other. – RealSkeptic Sep 20 '16 at 14:21
  • Note: Java language conventions say that class names should start with uppercase letters, and method names should start with lowercase letters. You should have named the *class* `Anagram`, and the method `isAnagram()`, not the other way around. – RealSkeptic Sep 20 '16 at 14:22
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – resueman Sep 20 '16 at 14:41
  • at which line you are getting NullPointerException. Comment beside the line of your code – Razib Sep 20 '16 at 14:42
  • Thank you all so much!!! I've solved this, I decided not to have two classes - Klasa and isAnagram, because I didn't connect them right, but to put everything in just one class, and it worked! – GandalfTheGray Sep 20 '16 at 17:21

1 Answers1

-1

Instead of using Scanner for opening a file,you should use FileReader/FileWriter classfor opening and editing the file.Then use Scanner class to read the text from the file.

Shadab Faiz
  • 2,380
  • 1
  • 18
  • 28