• Register
0 votes
30 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 46 views
46 views
How can I solve this?
asked Jan 4 TeamScript 13.5k points
0 votes
1 answer 10 views
10 views
Problem I need to check whether the object is null. In case it is not null process it, otherwise cancel processing. I am doing it this way: if (!price.Equals(null)) But I get NullReferenceException. If the object is null it just should not enter the if block. Obviously ... .Add(price); success = ProcessPrice(price); } } catch (Exception e) { throw new Exception(e.ToString()); } return success; }
asked Nov 6, 2020 alexh 2.6k points
0 votes
1 answer 3 views
3 views
Problem: I'm playing around with Java's reflection API and trying to handle some fields. Now I'm stuck with identifying the type of my fields.
asked May 3 salim1 60.6k points
0 votes
1 answer 5 views
5 views
Problem: Hi there! I have 3 classes in a program and I have declared multiple objects of each class in the main() method. I want to print the type of each object on the screen. Is there any mechanism in Java that could be used to detect the type of object? If yes, kindly elaborate with an example program. Else, any type of help would be appreciated. Thank!
asked Mar 31 Code Learner 9.9k points
0 votes
1 answer 8 views
8 views
Problem: So if you do: void *ptr = NULL; What is the best way to check if that void pointer is NULL? My workaround for now is this: if (*(void**)ptr == NULL) ... But this doesn't seem like the best way, as I'm implicitly assuming ptr is of type void** (which it isn't).
asked Dec 24, 2020 Mashhoodch 13k points
1 vote
1 answer 4 views
4 views
Problem: I need to find whether an object already exist in an array in Javascript? Javascript check if object is in array.
asked Mar 24 PkGuy 23.5k points
0 votes
1 answer 3 views
3 views
Problem: Is there a universal JavaScript function that checks that a variable has a value and ensures that it's not undefined or null?
asked May 2 Supti 13k points
0 votes
1 answer 4 views
4 views
Problem: How to check an integer value is Null in c#?
asked Apr 21 sumaiya simi 43.9k points
1 vote
1 answer 7 views
7 views
Problem: Can anyone tell me the solution of this problem? c# check if int is null
asked Apr 12 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 5 views
5 views
Poblem: Help me checking jquery wheather the variable is empty or null.
asked Apr 10 zayed1 61.8k points