• Register
0 votes
28 views
I tried to execute the methods of a null object.and i write a program given bellow and in this program i failed to call the method:
{ 
   
    public String getName()  
    { 
        return "Not Available"; 
    } 
  
   
    public boolean isNull()  
    { 
        return true; 
    } 
} 


   public class main {
        
     public static void main (String [] args){
       
       system.out.println(emp1.getname());

}

}

what is wrong in my code and how can I check if a object is null in java?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

NullObject :  null object class that can be used as a dependency by the Client. It contains no functionality but implements all of the members defined by the DependencyBase abstract class. You should have written like:

abstract class Emp 
{ 
    protected String name; 
    public abstract boolean isNull(); 
    public abstract String getName(); 
}

class NoClient extends Emp 
{ 
    @Override
    public String getName()  
    { 
        return "Not Available"; 
    } 
  
    @Override
    public boolean isNull()  
    { 
        return true; 
    } 
}

class EmpData {
public static final String [] names = {"fff"}
public static Emp getClint(String name)

{ 
        for (int i = 0; i < names.length; i++)  
        { 
            if (names[i].equalsIgnoreCase(name)) 
            { 
                return new Coder(name); 
            } 
        } 
        return new NoClient(); 
    } 
} 

}

public class Main {

     public static void main(String[] args)
{
     Emp emp1 = EmpData.getClient("fff"); 
     Emp emp2 = EmpData.getClient("xxx");

     System.out.println(emp1.getName()); 
      System.out.println(emp2.getName());       

}

}
 
11 6 4
34,950 points

Related questions

0 votes
1 answer 15 views
15 views
How can I solve this?
asked Jan 4 TeamScript 13.5k points
0 votes
1 answer 3 views
3 views
Problem: How would you check if a String was a number before parsing it?This is generally done with a simple user-defined function (i.e. Roll-your-own "isNumeric" function). How If it is, we can assume the entire string is numeric:?
asked Feb 15 Ali 4.3k points
0 votes
1 answer 15 views
15 views
Problem: I have an int array which has no elements and I'm trying to check whether it's empty. For example, why is the condition of the if-statement in the code below never true? int[] k = new int[3]; if (k == null) { System.out.println(k.length); }
asked Feb 3 Ali 4.3k points
0 votes
1 answer 14 views
14 views
How would you check if a String was a number before parsing it?
asked Jan 4 TeamScript 13.5k points
0 votes
1 answer 17 views
17 views
I was working on that but I got into some problems.
asked Nov 5, 2020 TeamScript 13.5k points
0 votes
1 answer 16 views
16 views
I was working on that but I got into some problems.
asked Nov 4, 2020 TeamScript 13.5k points
0 votes
1 answer 23 views
23 views
Problem: Hi! I want to check whether an array is empty or null in Java. Any guidance would be appreciated.
asked Oct 12, 2020 Code Learner 5.7k points
0 votes
1 answer 12 views
12 views
I need to check if a number is a perfect squre in java.
asked Oct 5, 2020 Daniel Anderson 4k points
1 vote
1 answer 38 views
38 views
Problem: Hello good people, I am a novice in java programming. I am learning java online and solving problems daily. Today I got a problem that says to check if input is a number. How can I check it? Is it require a scanner class? Could anybody here help me to solve this problem, please?
asked Apr 7, 2020 Gavin 15.3k points
0 votes
1 answer 35 views
35 views
I am very new at java programing, I have an assginment where I have to check if a character is uppercase in java and I wrote a code: public static void main(String[] args) { int A; char a = A; if (Character.isUpperCase()) { System.out.println(a + " is an uppercase character."); } } But it doesn&rsquo;t work how. Can anyone help me out?
asked Mar 15, 2020 LizzyM 6.1k points