Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object Array doubt,please help

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


public class Java_arrays{
public static void main(String[] args) {
Dog[][] theDogs=new Dog[3][];
System.out.println(theDogs[1][0].toString());
}
}

class Dog{
}


This code will give RunTimeError because theDogs[1][0] is having null value.I got that.
I tried to execute the above program with some value.Can anyone help me to figure out where I am doing mistake on the below program.


public class Java_arrays{
public static void main(String[] args) {
Dog[][] theDogs=new Dog[3][];
theDogs[0]=new Dog[2]; //assigning column

Dog theD=new Dog(5);

theDogs[0][1] =theD.p;

System.out.println(theDogs[1][0].toString());
}
}

class Dog{
int p;
Dog(int p){
this.p=p;
}
}


I would like to get the answer 5.Please help me.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this , this should work

public class java_arrays{
public static void main(String[] args) {
Dog[][] theDogs=new Dog[3][];
theDogs[0]=new Dog[2]; //assigning column

Dog theD=new Dog(5);

theDogs[0][1] =theD;

System.out.println(theDogs[0][1].toString());
}
}

class Dog{
int p;
Dog(int p){
this.p=p;
}
public String toString()
{
return ""+p;
}}

Good luck ,
santhosh sharma (scjp 1.4 100%)
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiva,

If u'll look at ur code carefully u'll be able to find out the problem...

In first line u've created a Dogs array

Dog[][] theDogs=new Dog[3][];

and then u r trying to assign it an int value

theDogs[0][1] =theD.p;

"p" is an int type instance variable of Dog class ... in the thedogs arrays u can assign only Dogs object not any other type objects ... unless they can implicitly be promoted to that type ...

Second problem is the array index .....

theDogs[1][0].toString()

in the above statement it shud be [0][1] ... as u have assigned the value at that position not at [1][0].

Hope it will help u ..

ashi
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Santhosh,Asha.It worked.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This code will give RunTimeError because theDogs[1][0] is having null value.I got that.


I think you misunderstand the null type
this code will give a NullPointerException my answer is because
theDogs[1] is having null value other than theDogs[1][0] is having null value.


[ February 16, 2006: Message edited by: Changchun Wang ]
[ February 16, 2006: Message edited by: Changchun Wang ]
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic