Did anybody please explain me wat is the problem here...
it is showing a null pointer exception... -
class array
-
{
-
String var;
-
public static void main(String args[])
-
{
-
array obj[];
-
obj = new array[1];
-
obj[0].var="1";
-
System.out.println(obj[0].var);
-
}
-
}
11 1527
Did anybody please explain me wat is the problem here...
it is showing a null pointer exception... -
class array
-
{
-
String var;
-
public static void main(String args[])
-
{
-
array obj[];
-
obj = new array[1];
-
obj[0].var="1";
-
System.out.println(obj[0].var);
-
}
-
}
friend, why don't you try such this: - class Array
-
{
-
public static void main(String args[])
-
{
-
String[] obj;
-
-
obj = new String[1];
-
-
obj[0] = "Hello;
-
System.out.println(obj[0]);
-
-
}
-
}
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: - class Array
-
{
-
public static void main(String args[])
-
{
-
String[] obj;
-
-
obj = new String[1];
-
-
obj[0] = "Hello;
-
System.out.println(obj[0]);
-
-
}
-
}
Did anybody please explain me wat is the problem here...
it is showing a null pointer exception... -
class array
-
{
-
String var;
-
public static void main(String args[])
-
{
-
array obj[];
-
obj = new array[1];
-
obj[0].var="1";
-
System.out.println(obj[0].var);
-
}
-
}
When you instantiate an array of type T, as in:
... 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: -
for (int i= 0; i < array.length; i++)
-
array[i]= new T(); // point to a new T object
kind regards,
Jos
i didnt get you
would you please make it clear,,,
When you instantiate an array of type T, as in:
... 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: -
for (int i= 0; i < array.length; i++)
-
array[i]= new T(); // point to a new T object
kind regards,
Jos
i didnt get you
would you please make it clear,,,
What particular part of my reply didn't you understand?
kind regards,
Jos
first of all "When you instantiate an array of type T, as in:"
i didnt understand that .. and how can i slove my prob.... -
class array
-
{
-
String var;
-
public static void main(String args[])
-
{
-
array obj[];
-
obj = new array[3];
-
obj[0].var="A";
-
System.out.println(obj[0].var);
-
}
-
}
-
What particular part of my reply didn't you understand?
kind regards,
Jos
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:
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: -
array[0]= new T(); // element 0 of the array now points to a T object
kind regards,
Jos
Thanx to all of them who helped me..
I got solution...
But would anybody please explain me that line i commented... -
class array
-
{
-
String var;
-
public static void main(String args[])
-
{
-
array obj[];
-
obj = new array[1];
-
obj[0]=new array(); // i didnt understand this line...please tell me in details...
-
obj[0].var="A";
-
System.out.println(obj[0].var);
-
}
-
}
-
Thanx to all of them who helped me..
I got solution...
But would anybody please explain me that line i commented... -
class array
-
{
-
String var;
-
public static void main(String args[])
-
{
-
array obj[];
-
obj = new array[1];
-
obj[0]=new array(); // i didnt understand this line...please tell me in details...
-
obj[0].var="A";
-
System.out.println(obj[0].var);
-
}
-
}
-
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
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
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: -
T[] a; // an array; each element has type T
-
a= new T[42]; // make an array with 42 T references
-
a[0]= new T(); // the first element points to a type T object now
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
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\"...
|
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...
|
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...
|
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...
|
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...
|
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"};
//...
}
|
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>...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |