Home > Enterprise >  How to get Null values in this Java programme
How to get Null values in this Java programme

Time:09-03

package home;

public class Home {
    String HomeName = null;
        int room = 0;
        int kitchen = 0;
        int bathroom =0;
    
    //Constructor
    Home(String HomeName){
        System.out.println(HomeName);
    }
    Home(String HomeName, int rooms, int k ,int bathroom){
        System.out.println(HomeName ":rooms:" rooms ":kitchen" k ":bathroom" bathroom);
        this.HomeName = HomeName;
        this.room = rooms;
        this.kitchen = k;
        this.bathroom = bathroom;
    
    }
    public void finaloutputhome(){
        System.out.println("Your Home Name:" HomeName);
        System.out.println("Rooms Count:" room);
        System.out.println("kitchen Count:" kitchen);
        System.out.println("Bathroom Count:" bathroom);
    
    }
        
    public static void main(String[] args) {
        
        Home obj2 = new Home();
        obj2.finaloutputhome();
        
        
    }
    
}

Error Code==>

*run:

Exception in thread "main" java.lang.RuntimeException: Uncompilable code - no suitable constructor found for Home(no arguments)
    constructor home.Home.Home(java.lang.String) is not applicable
      (actual and formal argument lists differ in length)
    constructor home.Home.Home(java.lang.String,int,int,int) is not applicable
      (actual and formal argument lists differ in length)
    at home.Home.main(Home.java:1)
C:\Users\donra\AppData\Local\NetBeans\Cache\13\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\donra\AppData\Local\NetBeans\Cache\13\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)*

The output I need===>>

Pasindu's Home:rooms:5:kitchen2:bathroom3
Your Home Name:null
Rooms Count:0
kitchen Count:0
Bathroom Count:0

CodePudding user response:

You should either pass arguments when you create a Home:

new Home(null)

or you should add a constructor that takes no arguments:

public Home() {
}
  • Related