Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:20:28+00:00 2026-05-26T05:20:28+00:00

public boolean isItMyName(String name) { name = fullname.substring(0,fullname.indexOf(‘ ‘)); … } Then my code

  • 0
public boolean isItMyName(String name)
    {    
       name = fullname.substring(0,fullname.indexOf(' '));
       ...
    }

Then my code goes on and on. When I run this, I get a null pointer exception and the error is pointing to this line.

I also created a JUnit tester, where it tests whether the input is true or false.

Name myName = new Name();
assertEquals(false, myName.isItMyName("Troll");

It also gives me a null pointer exception at assertEquals(false, myName.isItMyName("Troll"));

I’m at my wit’s end. How do I fix this?

@ Greg Hewgill:

In my Name class, I do this:

public class Name(String n)
{
  name = n;
}

But in my main method, I pass in the string: Name myName = new Name(“Johnathy”);

So string isn’t null.

FULLNAME:

public class EmailAddress {
private String address;



public EmailAddress(String a)
{
  address = a;

}

public String toString()
{
    return address;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:20:29+00:00Added an answer on May 26, 2026 at 5:20 am

    Well you’re ignoring your parameter… you’re assigning a new value to name in the first line. That means your method is almost certainly broken to start with.

    Then consider where fullname (which I assume is an instance variable) is going to come from. Unless you’re setting it to a value within your parameterless constructor, it’s going to be null by default. Is that really what you want?

    I suggest you change your Name class to only have constructors taking name parameters, and that you validate that they’re not null. That way you can never have an invalid Name object, where the full name is null.

    EDIT: Now you’ve posted more code, look at your parameterless constructor and the setName method:

    public Name()
    {
       setName(fullname);     
    }
    
    public void setName(String n)
    {
        fullname = n;
    }
    

    That means your parameterless constructor is equivalent to:

    public Name()
    {
       fullname = fullname;
    }
    

    … i.e. a no-op. fullname will be null, hence the exception.

    My advice is to get rid of the parameterless constructor entirely, and make the other one validate the parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code for update: public Boolean update() { try { data.put(ContactsContract.Groups.SHOULD_SYNC, true);
Why doesn't this code compile? public boolean isOf(Class clazz, Object obj){ if(obj instanceof clazz){
I have this code public static Boolean freq[] = new Boolean[Global.iParameter[2]]; freq[Global.iParameter[2]] = false;
I have the following code: public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[]
I am using this code: public boolean onKey(View v, int keyCode, KeyEvent event) {
I have the following code in a java Web Service: public boolean makeFile(String fileName,
Asked an unrelated question where I had code like this: public boolean equals(Object obj)
What does this code mean: table = new JTable(){ public boolean isCellEditable(int arg0, int
I want some help converting this Java code to an if/else statement. public boolean
evertime i use this code it says theres a nullpointer exception public boolean onTouchEvent(MotionEvent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.