473,385 Members | 2,269 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,385 software developers and data experts.

Null pointer Exception

50
Hi..

I get a null pointer exception error. Please give me a solution as how to access this. The code snippet is as follows.
Expand|Select|Wrap|Line Numbers
  1.  public class x
  2. {
  3.     private String y;
  4.  
  5.     public String gety()
  6.     {
  7.         return y;
  8.     }
  9.  
  10.     public void sety(String m)
  11.     {
  12.         this.y = m ;
  13.     }
  14.  
  15. }
  16.  
  17.  
  18. public class a
  19. {
  20.  
  21.     x ax = new x();
  22.     ax.sety("Hai");
  23.  
  24. }
  25.  
  26.  
  27. public class z
  28. {
  29.     a az = new a();
  30.     System.out.println(za.ax.gety()); //null Pointer exeption
  31.  
  32. }
Apr 28 '09 #1
5 2359
r035198x
13,262 8TB
Are you sure that your code compiles?
Also read the NullPointerException article.
Apr 28 '09 #2
uhdam
50
Yes sir.. it does...
Apr 28 '09 #3
r035198x
13,262 8TB
Classes a and z do not compile.
Apr 28 '09 #4
JosAH
11,448 Expert 8TB
@uhdam
Don't lie. The code you've shown us doesn't compile; no discussion about it.

kind regards,

Jos
Apr 28 '09 #5
@uhdam
Correct code for class a and claas z as follows -

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Z {
  3.     public static void main(String a[]){
  4.         A az = new A();
  5. System.out.println(az.ax.getY()); //null Pointer exeption
  6.   }
  7. }
  8.  
  9. class A
  10. {
  11.     X ax;
  12.     A(){
  13.     ax = new X();
  14.     ax.setY("Hai");
  15.       }
  16.  
  17. }
  18.  
  19.  
  20. class X
  21. {
  22.     private String y;
  23.  
  24.     public String getY()
  25.     {
  26.         return y;
  27.     }
  28.  
  29.     public void setY(String m)
  30.     {
  31.         this.y = m ;
  32.     }
  33.  
  34. }
  35.  
  36.  
Now I think urn code will compile and run...
Apr 28 '09 #6

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

Similar topics

102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
5
by: Boniek | last post by:
Hi I define a public property in a new form and I can see this property in table of Properties in Visual. How I can hide this property to see only in code ? Thank's Boniek
5
by: Tony Cheng | last post by:
for (int i=0; i<_request.Form.Count; i++ ) { string key = _request.Form.GetKey(i); if ( !key.Equals("formCode") && !key.Equals("formLanguage") && !key.Equals("__VIEWSTATE") &&...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
18
by: Denis Petronenko | last post by:
Hello, in the following code i have segmentaion fault instead of exception. Why? What i must to do to catch exceptions in such situation? Used compiler: gcc version 3.3.6 (Debian 1:3.3.6-13) ...
3
by: Michael | last post by:
Hi, Could you tell me how to under the following statements? Does it mean each and every memory allocation will check all the pointer no matter a null pointer or not? Right? Thanks in advance! ...
1
by: zahidkhan | last post by:
Hi All, Plz help me if you can..... I have a program something like this int main(int argc,char* argv) { try { int* p = NULL;
46
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.