my program is compiled.but on run that i have message "Exception in thread "main" jav

Reply

Join Date: Feb 2007
Posts: 52
Reputation: ajay_tabbu is an unknown quantity at this point 
Solved Threads: 0
ajay_tabbu's Avatar
ajay_tabbu ajay_tabbu is offline Offline
Junior Poster in Training

my program is compiled.but on run that i have message "Exception in thread "main" jav

 
0
  #1
Feb 7th, 2007
my program is compiled.but on run that i have message "Exception in thread "main" java.lang.NullPointer Exception"



1. class Rev
2. {
3. char a[];
4. char i=3;
5. void str()
6. {
7. for(i='A';i<'D';i++)
8. {
9. a[i]='i';
10. }
11. }
12. void show()
13. {
14. for(int i='A';i<'D';i++)
15. {
16. System.out.print(a[i]);
17. }
18. }
19. void reversStr()
20. {
21. for(i='A';i<'D';i++)
22. {
23. System.out.print(a[i]);
24. }
25. }
26. }
27. class Revers
28. {
29. public static void main(String[] args)
30. {
31. Rev a1=new Rev();
32. a1.str();
33. a1.show();
34. a1.reversStr();
35. }
36. }
37. plz run and solve the problem
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,189
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: my program is compiled.but on run that i have message "Exception in thread "main"

 
1
  #2
Feb 7th, 2007
I think you should reconsider your for loop

  1. for(i='A';i<'D';i++)

I never heard that in java you can increase value of character type variable, this is possible but in Perl
Create array of characters and with use of "i" get the character from the array
so if char[] myArray = { 'A', 'B', 'C'}
then if i = 1
you get character B
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 74
Reputation: Cudmore is an unknown quantity at this point 
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Re: my program is compiled.but on run that i have message "Exception in thread "main"

 
1
  #3
Feb 7th, 2007
Oh my.

For one, I think this is how you meant to represent your code:
  1. class Rev {
  2. char a[];
  3. char i=3;
  4.  
  5. void str() {
  6. for(i='A';i<'D';i++) {
  7. a[i]='i';
  8. }
  9. }
  10.  
  11. void show() {
  12. for(int i='A';i<'D';i++) {
  13. System.out.print(a[i]);
  14. }
  15. }
  16.  
  17. void reversStr() {
  18. for(i='A';i<'D';i++) {
  19. System.out.print(a[i]);
  20. }
  21. }
  22.  
  23. }
  24.  
  25. class Revers {
  26.  
  27. public static void main(String[] args) {
  28. Rev a1=new Rev();
  29. a1.str();
  30. a1.show();
  31. a1.reversStr();
  32. }
  33.  
  34. }

And, I get the error because you're trying to access an array element without having defined the size of the array.

  1. void str() {
  2. for(i='A';i<'D';i++) {
  3. a[i]='i'; // ERROR OCCURS HERE
  4. }
  5. }

So, define a length for the array at the start of class Rev: char[] a = new char[(some integer)];

You're still going to end up with issues, such as java.lang.ArrayIndexOutOfBoundsException: 65 when you try to access a['A'] and so on, because 'A' as a char is 65 as an integer, and integers are what you want to work with when accessing arrays.

I have no idea what your program's trying to do, but those are some thing to consider.
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 52
Reputation: ajay_tabbu is an unknown quantity at this point 
Solved Threads: 0
ajay_tabbu's Avatar
ajay_tabbu ajay_tabbu is offline Offline
Junior Poster in Training

Re: my program is compiled.but on run that i have message "Exception in thread "main"

 
0
  #4
Feb 7th, 2007
thank you very much
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 52
Reputation: ajay_tabbu is an unknown quantity at this point 
Solved Threads: 0
ajay_tabbu's Avatar
ajay_tabbu ajay_tabbu is offline Offline
Junior Poster in Training

Re: my program is compiled.but on run that i have message "Exception in thread "main"

 
0
  #5
Feb 7th, 2007
i m very thankfull 4 r u r help
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC