• Register
0 votes
25 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,050 points

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,930 points

Related questions

0 votes
1 answer 11 views
11 views
Problem: Hi! I want to check whether an array is empty or null in Java. Any guidance would be appreciated.
asked Oct 12 Sheeza 2.3k points
0 votes
1 answer 2 views
2 views
I need to check if a number is a perfect squre in java.
asked Oct 5 Daniel Anderson 4k points
1 vote
1 answer 22 views
22 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 Gavin 15.3k points
0 votes
1 answer 28 views
28 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 LizzyM 6.1k points
0 votes
1 answer 17 views
17 views
I was doing my java programming practice. I wrote a code to check if a charcter is a number: public static void main(String[] args) { String string = "-1234.15"; String numeric ; numeric = string.matches("-?\\d+(\\.\\d+)?"); if(numeric) System.out.println(string + " is a number"); else System.out.println(string + " is not a number"); } But it gives me error. Whats wrong in my code?
asked Mar 15 LizzyM 6.1k points