0

I have a simple two class program in java and the main class doesont want to create an object of the nameProcessor class

I tried everything I know but I am still a dumb begginer

public class Main
{
    public static void Main()
    {
        // initialise instance variables

        String name="Andy";
        System.out.println("That name is "+name.length()+" letters long");
        System.out.println("The first letter is "+name.substring(0,1));
        nameProcessor np1 = new nameProcessor(name);
        if (np1.nameChecker()==false)
           System.out.println("That is a nice name.");
        else
            System.out.println("That is a wierd name.");
    }
}

public class nameProcessor
{
    private String userName;
    private String letters;
    private String letter;
    private boolean give;


    public nameProcessor(String name)
    {
        // initialise instance variables
        String userName=name;
        String letters="XYZWxyzw";
        String letter="";
        boolean give=false;
    }


    public boolean nameChecker()
    {
        give=false;
        for(int i=0;i<userName.length();i++){
            letter=letters.substring(i,i+1);
            if (userName.indexOf(letter)!=-1){
                give=true;
            }
        }
        return give;
    }
}

//Some correct outputs would be

//Andy->That is a nice name //XXX->that is a wierd name

//just any string containing XYZWxyzw should return true

AX-11
  • 41
  • 4
  • 1
    Have a look at `nameProcessor(String name)`: you're hiding the fields with local variables of the same name so the initialization won't happen. Change `String userName=name;` to `userName=name;` etc. – Thomas Oct 24 '19 at 11:45
  • @GuilhermeMartin why didn't you flag it as a duplicate? – Stultuske Oct 24 '19 at 11:46
  • @Stultuske Already did that – Guilherme Martin Oct 24 '19 at 11:48
  • Your main method is missing String args[] as params. public static void main(String args[]) {} – deadzg_devil Oct 24 '19 at 11:48
  • @GuilhermeMartin I saw your post that it's a Possible duplicate, but nothing in the flags themselves .. I still don't. according to the counter, I'm the only one who flagged it to close it. – Stultuske Oct 24 '19 at 11:49
  • @Stultuske duplicate of What is a NullPointerException, and how do I fix it? – Guilherme Martin 20 mins ago helpful – Guilherme Martin Oct 24 '19 at 12:05
  • @GuilhermeMartin yes. but did you type that comment with that link? According to SO only Jachim_Sauer and I marked it a duplicate. Might be a glitch in the system, though. – Stultuske Oct 24 '19 at 12:07
  • @Stultuske Whenever I mark something as a duplicate, it comments automatically. – Guilherme Martin Oct 24 '19 at 12:18
  • @GuilhermeMartin yes, I know it does that, but normally it also marks you as someone who marked it off topic as a duplicate, that doesn't seem to work always. – Stultuske Oct 24 '19 at 12:19

1 Answers1

1

You are again redefining the variables instead of assigning to the class members

 public nameProcessor(String name)
    {
        // initialise instance variables
        String userName=name; // you are resintializing the local variables 
        String letters="XYZWxyzw";
        String letter="";
        boolean give=false;
    }

because of this your method nameChecker() is getting userName as null hence null pointer exception.

This should be

public nameProcessor(String name)
        {
            // initialise instance variables
            this.userName=name; // you are resintializing the local variables 
            this.letters="XYZWxyzw";
            ...
        }
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37