Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help on a basic matter

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    Toronto
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need help on a basic matter

    Hello,

    I'm trying to build a basic java program to test what i know.
    So far, it's not going anywhere.. lol

    See my code below, as I have 1 main class called HOUSE, and subclasses called living.. kitchen.. and whatever
    I have input variable like lenght and width and trying to "getarea". the result i'm getting is NULL.. and i don'T know why

    Any ideas, and thanks for any support you may provide.

    /**
    *
    * @author Daniel
    */
    public class AlexHouse1 {
    private static int Height = 12; // The height is the same for all rooms
    private static int Lenght;
    private static int Width;
    private static int floors;
    private static int HouseNumber = 33; // The House civic number
    private static int HouseRooms = 4; // Number of room into the House
    private static int YearConstruction = 2001;
    private static String HouseStreet = "My street name"; // The Street name
    private static String HouseCity = "Toronto"; // City Name
    private static String HouseSize;
    int temperature; // It's temperature, but i have not assiged value.
    String HouseColor = "yellow"; // Outside color of the house


    public class AlexHouse1Size {
    public int Lenght = 0;
    public int Width = 0;
    public AlexHouse1Size(int a, int b){
    Lenght = a;
    Width = b;
    }


    public int getArea() {
    return Lenght * Width;
    }
    AlexHouse1Size HouseSize = new AlexHouse1Size(12,15);

    }






    public class LivingRoom extends AlexHouse1 { // Subclaass Living Room
    int LivingRoomLenght;
    int LivingRoomWidth;
    int LivingRoomLHeight = 8;
    int LivingRoomSize;
    String LivingRoomColor = "beige";
    int LivingRoomTemperature;

    }


    public static void main(String[] args) {
    AlexHouse1 House = new AlexHouse1(); // To create AlexHouse "object"



    System.out.println("Alex House have a total of " + HouseRooms + " rooms");
    System.out.println("and she was built in " + YearConstruction);
    System.out.println("The house total height is " + Height);
    System.out.println("The house size is " + HouseSize);
    }

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help on a basic matter

    You're trying to use static variables as member variables. Instead, you should be doing something like:

    AlexHouse1 house = new AlexHouse1();
    System.out.println("Alex House have a total of " + house.getHouseRooms() + " rooms");

    Also, you should be following standard naming conventions- variables and methods start with a lower-case letter. It might not seem like a big deal, but it makes it so much easier to read.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    Toronto
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help on a basic matter

    Ok, I don't know why i put capital letters everywhere.. I corrected that.

    with this code:

    /**
    *
    * @author Daniel
    */
    public class AlexHouse1 {
    private static int height = 12; // The height is the same for all rooms
    private static int lenght;
    private static int width;
    private static int floors;
    private static int houseNumber = 33; // The House civic number
    private static int houseRooms = 4; // Number of room into the House
    private static int yearConstruction = 2001;
    private static String houseStreet = "My street name"; // The Street name
    private static String houseCity = "Toronto"; // City Name
    private static String houseSize;
    int temperature; // It's temperature, but i have not assiged value.
    String houseColor = "yellow"; // Outside color of the house


    public class AlexHouse1Size {
    public int lenght = 0;
    public int width = 0;
    public AlexHouse1Size(int a, int b){
    lenght = a;
    width = b;
    }


    public int getArea() {
    return lenght * width;
    }
    // AlexHouse1Size HouseSize = new AlexHouse1Size(12,15);

    }






    public class LivingRoom extends AlexHouse1 { // Subclaass Living Room
    int livingRoomLenght;
    int livingRoomWidth;
    int livingRoomLHeight = 8;
    int livingRoomSize;
    String livingRoomColor = "beige";
    int livingRoomTemperature;

    }


    public static void main(String[] args) {
    AlexHouse1 house = new AlexHouse1(); // To create AlexHouse "object"



    System.out.println("Alex House have a total of " + houseRooms + " rooms");
    System.out.println("and she was built in " + yearConstruction);
    System.out.println("The house total height is " + height);
    System.out.println("The house size is " + houseSize);
    }

    }

    the result is :
    run:
    Alex House have a total of 4 rooms
    and she was built in 2001
    The house total height is 12
    The house size is null
    BUILD SUCCESSFUL (total time: 0 seconds)

    I don't get why it's null..
    Perhaps i should start from scratch all over.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help on a basic matter

    Right, that's because you're still using those static variables. Those should not be static. And you should not be accessing them directly. They need to belong to an instance of AlexHouse1.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help on a basic matter

    Another question:
    Where do you assign a value to the variable that is printing out as null?
    If you don't give a String variable a value, its value will be null.

Similar Threads

  1. BASIC program help
    By ryanquanz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2011, 10:42 AM
  2. Need basic String help
    By Sadalmelik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2011, 06:05 AM
  3. Rounding long or floating numbers, or longs for that matter.
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2011, 12:32 AM
  4. Basic Animation
    By tabutcher in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 10:07 AM
  5. Basic Beginner Help
    By SRD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:27 PM