Hi guys,

i am new to java so i started of with basics, but now im stuck at a little problem. I will paste the code and error's below, hoping that some1 could sort em out

import java.lang.*;
import java.io.*;
class Sphere{
         float radius;
         int cXcord;
         int cYcord;
         int cZcord;
        
        Sphere()
        {
            radius=5;
            cXcord=0;
            cYcord=0;
            cZcord=0;
        }
        
        Sphere(float r,int cX,int cY,int cZ)
        {
            radius = r;
            cXcord = cX;
            cYcord = cY;
            cZcord = cZ;
            
        }
        
        double SurfaceArea()
        {
            return ( 4*3.14*radius*radius);
        }
        
        double Volume()
        {
            return ((4*3.14*radius*radius*radius)/3);
        }        
    }
    
    class ColorBall extends Sphere
    {
        String color = new String();
        
        ColorBall()
        {
            super();
            color = "White";
        }
        ColorBall(String col)
        {
            super();
            color = col;
        }
        ColorBall(float r,int cX,int cY,int cZ,String col)
        {
            super(r,cX,cY,cZ);
            color = col;
        }
        String getColor()
        {
            return color;
        }
    }
    
    class HeavyBall extends ColorBall
    {
        int weight;
        HeavyBall()
        {
            super();
            weight=1;
        }
        HeavyBall (int w)
        {
            super();
            weight = w;            
        }
        HeavyBall(float r,int cX,int cY,int cZ,String col,int w)
        { 
          super(r,cX,cY,cZ,col);
          weight=w;    
            
        }
        
        int getWeight()
        {
            return weight;
        }
    }
    public class Main{
        
    
    
        public static void main(String args[])
        {
           int eValue,cX,cY,cZ,weight,w1,w2;
           float radius;
           String color,c1,c2;
           //HeavyBall obj1 = new HeavyBall(radius,cX,cY,cZ,color,weight);
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           System.out.print("Press 1 to enter sphere details , 0 to go on with default values for Sphere 1");
           try {
               eValue=br.read();
           
           System.out.println();
           if (eValue==1)
           {
               System.out.print("Enter Radius : ");
               radius = Integer.parseInt(br.readLine());
               System.out.println();
               System.out.print("Enter X Co-ordinate for center : ");
               cX = br.read();
               System.out.println();
               System.out.print("Enter Y Co-ordinate for center");
               cY = br.read();
               System.out.println();
               System.out.print("Enter Z Co-ordinate for center");
               cZ = br.read();
               System.out.println();
               System.out.print("Enter Color of the Sphere");
               color = br.readLine();
               System.out.println();
               System.out.print("Enter Weight of the Sphere");
               weight = Integer.parseInt(br.readLine());
               System.out.println();
               HeavyBall obj1 = new HeavyBall(radius,cX,cY,cZ,color,weight);
           }
             
           else if (eValue==0)
           {
                HeavyBall obj1 = new HeavyBall();
           }
           else
           {
               System.out.println("Wrong choice!!!");
               
           }
           
              
           
           System.out.print("Press 1 to enter sphere details , 0 to go on with default values for Sphere 2");
           eValue=br.read();
           
           System.out.println();
           if (eValue==1)
           {
               System.out.print("Enter Radius : ");
               radius = Integer.parseInt(br.readLine());
               System.out.println();
               System.out.print("Enter X Co-ordinate for center : ");
               cX = br.read();
               System.out.println();
               System.out.print("Enter Y Co-ordinate for center");
               cY = br.read();
               System.out.println();
               System.out.print("Enter Z Co-ordinate for center");
               cZ = br.read();
               System.out.println();
               System.out.print("Enter Color of the Sphere");
               color = br.readLine();
               System.out.println();
               System.out.print("Enter Weight of the Sphere");
               weight = Integer.parseInt(br.readLine());
               System.out.println();
               HeavyBall obj2 = new HeavyBall(radius,cX,cY,cZ,color,weight);
           }
             
           else if (eValue==0)
           {
                HeavyBall obj2 = new HeavyBall();
           }
           else
           {
               System.out.println("Wrong choice!!!");
             
           }
           
           }
           catch(IOException e)
               {
                    System.out.println("Stream cannot be read :( exception caught");
               }
      System.out.println("Comparing Weights of the 2 sphere");
      w1=obj1.getWeight();
      w2=obj2.getWeight();
      
      
      if(w1>w2)
      {
           System.out.println("Sphere 1 is heavier than Sphere 2");
      }
            if(w2>w1)
      {
           System.out.println("Sphere 2 is heavier than Sphere 1");
      }
            if(w1==w2)
      {
           System.out.println("Sphere 1 weighs same as Sphere 2");
      }
      
      c1=obj1.getColor();
      c2=obj2.getColor();
      
      System.out.println("Color of sphere 1 is "+c1);
      System.out.println("Color of sphere 2 is "+c2);
           
           }
            
            
        }

errors that i get :

symbol : variable obj1
location: class Main
w1=obj1.getWeight();

same errors i get for object w2 , c1 and c2

Thnx in advance :)

Your problem is line 128 and 167 where you declare and initialize these objects with null value. It will be relatively fine if you use these objects just inside your else if statements and provide proper values for them, but not in your case when you use them later on outside these statements.
So declare your objects along with other variables (line 93 - 95) and then you can initialize them later in your code.

PS: You will have to sort few other errors in order get this working

Decleared the objects with other variables.
and its wrkin fine :-)

Thnx for the help

This question has already been answered. Start a new discussion instead.