473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Array probs

80
Did anybody please explain me wat is the problem here...
it is showing a null pointer exception...
Expand|Select|Wrap|Line Numbers
  1. class array
  2. {
  3. String var;
  4. public static void main(String args[])
  5.     {
  6.     array obj[];
  7.     obj = new array[1];
  8.     obj[0].var="1";
  9.     System.out.println(obj[0].var);
  10.     }
  11. }
Apr 6 '07 #1
11 1527
shana07
281 100+
Did anybody please explain me wat is the problem here...
it is showing a null pointer exception...
Expand|Select|Wrap|Line Numbers
  1. class array
  2. {
  3. String var;
  4. public static void main(String args[])
  5.     {
  6.     array obj[];
  7.     obj = new array[1];
  8.     obj[0].var="1";
  9.     System.out.println(obj[0].var);
  10.     }
  11. }
friend, why don't you try such this:
Expand|Select|Wrap|Line Numbers
  1. class Array
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String[] obj;
  6.  
  7.         obj = new String[1];
  8.  
  9.             obj[0] = "Hello;
  10.                    System.out.println(obj[0]);    
  11.  
  12.     }
  13. }
Apr 6 '07 #2
reon
80
My dear friend
Try to understand wat i mean , i am calling that variable named ' var ' from the array class...
Think before you post....

friend, why don't you try such this:
Expand|Select|Wrap|Line Numbers
  1. class Array
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String[] obj;
  6.  
  7.         obj = new String[1];
  8.  
  9.             obj[0] = "Hello;
  10.                    System.out.println(obj[0]);    
  11.  
  12.     }
  13. }
Apr 6 '07 #3
JosAH
11,448 Expert 8TB
Did anybody please explain me wat is the problem here...
it is showing a null pointer exception...
Expand|Select|Wrap|Line Numbers
  1. class array
  2. {
  3. String var;
  4. public static void main(String args[])
  5.     {
  6.     array obj[];
  7.     obj = new array[1];
  8.     obj[0].var="1";
  9.     System.out.println(obj[0].var);
  10.     }
  11. }
When you instantiate an array of type T, as in:
Expand|Select|Wrap|Line Numbers
  1. T[] array= new T[42];
... you have created an array of (in this example) 42 references to objects of
type T. They (the references) don't point to anything yet; they are all null.
If you want your references to point to objects of type T you have to make them
do that explicitly as in:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < array.length; i++)
  2.    array[i]= new T(); // point to a new T object
kind regards,

Jos
Apr 6 '07 #4
reon
80
i didnt get you
would you please make it clear,,,

When you instantiate an array of type T, as in:
Expand|Select|Wrap|Line Numbers
  1. T[] array= new T[42];
... you have created an array of (in this example) 42 references to objects of
type T. They (the references) don't point to anything yet; they are all null.
If you want your references to point to objects of type T you have to make them
do that explicitly as in:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 0; i < array.length; i++)
  2.    array[i]= new T(); // point to a new T object
kind regards,

Jos
Apr 6 '07 #5
JosAH
11,448 Expert 8TB
i didnt get you
would you please make it clear,,,
What particular part of my reply didn't you understand?

kind regards,

Jos
Apr 6 '07 #6
reon
80
first of all "When you instantiate an array of type T, as in:"
i didnt understand that .. and how can i slove my prob....

Expand|Select|Wrap|Line Numbers
  1. class array
  2. {
  3. String var;
  4. public static void main(String args[])
  5.     {
  6.     array obj[];
  7.     obj = new array[3];
  8.     obj[0].var="A";
  9.     System.out.println(obj[0].var);
  10.     }
  11. }
  12.  
What particular part of my reply didn't you understand?

kind regards,

Jos
Apr 6 '07 #7
JosAH
11,448 Expert 8TB
first of all "When you instantiate an array of type T, as in:"
i didnt understand that .. and how can i slove my prob....
T is a type and I want an array of that type, so I create (instantiate) one like this:
Expand|Select|Wrap|Line Numbers
  1. T[] array= new T[42];
Now I have an array with 42 references to a type T. These references don't point
to anything yet; you have to do that yourself. In your code you just created the
references (you 'new'd the array) but you didn't make any reference in that
array point to something. You should create an object to point to, as in:
Expand|Select|Wrap|Line Numbers
  1. array[0]= new T(); // element 0 of the array now points to a T object
kind regards,

Jos
Apr 6 '07 #8
reon
80
Thanx to all of them who helped me..
I got solution...

But would anybody please explain me that line i commented...


Expand|Select|Wrap|Line Numbers
  1. class array
  2. {
  3. String var;
  4. public static void main(String args[])
  5.     {
  6.     array obj[];
  7.     obj = new array[1];
  8.     obj[0]=new array(); // i didnt understand this line...please tell me in details...
  9.     obj[0].var="A";
  10.     System.out.println(obj[0].var);
  11.     }
  12. }
  13.  
Apr 6 '07 #9
JosAH
11,448 Expert 8TB
Thanx to all of them who helped me..
I got solution...

But would anybody please explain me that line i commented...


Expand|Select|Wrap|Line Numbers
  1. class array
  2. {
  3. String var;
  4. public static void main(String args[])
  5.     {
  6.     array obj[];
  7.     obj = new array[1];
  8.     obj[0]=new array(); // i didnt understand this line...please tell me in details...
  9.     obj[0].var="A";
  10.     System.out.println(obj[0].var);
  11.     }
  12. }
  13.  
That's the line where an object actually is created (by using the 'new' operator).
The reference in the first array slot points to this object now. btw, naming a
user defined type 'array' is very confusing, that's the reason I used the more
abstract type 'T' in my previous replies.

kind regards,

Jos
Apr 6 '07 #10
reon
80
Thanx ..
Is that T abstract type is more easy to understand would you please complete the code for me i didnt understand that(T abstract type) ....
if you demonstrate it with that particular code it might be able to quickly understandable..


That's the line where an object actually is created (by using the 'new' operator).
The reference in the first array slot points to this object now. btw, naming a
user defined type 'array' is very confusing, that's the reason I used the more
abstract type 'T' in my previous replies.

kind regards,

Jos
Apr 6 '07 #11
JosAH
11,448 Expert 8TB
Thanx ..
Is that T abstract type is more easy to understand would you please complete the code for me i didnt understand that(T abstract type) ....
if you demonstrate it with that particular code it might be able to quickly understandable..
Ok, a small code snippet can't do much harm:
Expand|Select|Wrap|Line Numbers
  1. T[] a; // an array; each element has type T
  2. a= new T[42]; // make an array with 42 T references
  3. a[0]= new T(); // the first element points to a type T object now
kind regards,

Jos
Apr 6 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kay Molkenthin | last post by:
Hello, I have a form with a dynamic checkbox structure: for ($ix=1; $ix<=$anzahl; $ix++) { echo "<TR><TD align=right>$ix.</TD><TD ALIGN=\"center\">"; echo "<input type=\"radio\"...
1
by: Arijit Chatterjee | last post by:
Hi Everybody, I am facing another probs. I have created a trigger for table Tab1 for perticular column col1 for checking value ranges.But at time for using insert statement it is working fine but...
2
by: andrewcw | last post by:
Seems it should be simple. I had problems with a larger class and the newsgroup suggested something I also tried. So I pruned my class to see if I could find out what could be wrong, Still dont...
9
by: Haobin | last post by:
Hi everyone, I have an ArrayList whose members are classes. These classes are derived from a same base class. The base class has a floating point array and a string. How do I access each element in...
7
by: polecat | last post by:
Im new to .NET and I am having probs with this Every time I reload the form the same data is repeated again.... Done reading on msdn and another forum cant figure out why the...
20
by: silverburgh.meryl | last post by:
In my code, I have an array of char* pointer which is populated statically: void function1() { char *ppsz_argv2 = { "abc" , "def", "dummy"}; //... }
6
by: Active8 | last post by:
Hi: This prob "magically" stopped and I'm not sure if I can recreate it, but thought I'd share it and maybe learn something. #include "stdafx.h" #include <stdlib.h> #include <math.h>...
0
by: bkpatel | last post by:
get Join the PHP community and share ur probs - soln <table border=0 style="background-color: #fff; padding: 5px;" cellspacing=0> <tr><td> <img...
2
by: brettokumar | last post by:
hai im storing value in cookies. if my 2nd value or 3rd value is stored means my previous cookies are expired my current cookies only stored others cookies values will be null how to i solve...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.