I've been trying to create an array of strings in Java. Here's my code (only the parts giving me problems):

In Class A:

Code:
public class A{
 private String a[10];

 public void changeElement(String e, int num){
  a[num]=e;
 }

}


Here's my application code:

Code:
import Java.util.Scanner;
public class App{
 Scanner reader=new Scanner(System.in);
 System.out.print("Elements(less than 10):");
 int j=reader.nextInt();
 A test=new A();
 for(int i=0; i<j; i++){
  While(reader.nextLine()=="\n");
  System.out.print("\nInput element "+i+":");
  A.changeElement(reader.nextLine(),i);
 }
}


What's wrong with this? It compiles fine, but throws a runtime error pointing to changeElement when I when I try to run this code and it reaches that point.
My guess is because you are saying A.changeElement() instead of test.changeElement() Razz

But why are you encapsulating a string array in a class anyway? Tad bit of overkill...
not sure on the error, but have you tried eclipse out for development?
Calc Buffoon wrote:
I've been trying to create an array of strings in Java. Here's my code (only the parts giving me problems):

In Class A:

Code:
public class A{
 private String a[10];

 public void changeElement(String e, int num){
  a[num]=e;
 }

}


Here's my application code:

Code:
import Java.util.Scanner;
public class App{
 Scanner reader=new Scanner(System.in);
 System.out.print("Elements(less than 10):");
 int j=reader.nextInt();
 A test=new A();
 for(int i=0; i<j; i++){
  While(reader.nextLine()=="\n");
  System.out.print("\nInput element "+i+":");
  A.changeElement(reader.nextLine(),i);
 }
}


What's wrong with this? It compiles fine, but throws a runtime error pointing to changeElement when I when I try to run this code and it reaches that point.



what IS the runtime error?
elfprince13 wrote:
what IS the runtime error?


Do you really even need to know that? Look at this line. I don't even know Java and I see a glaring problem with it


Code:
A.changeElement(reader.nextLine(),i);


Java would be pretty stupid if you created a variable called test (of class A) and then accessed it using A, instead of by its name (test) Rolling Eyes

...which is, of course, what I said in my first post. Or did you miss that?
oops, that's a typo on my part. the code really was supposed to be test.ChangeElement.
I found out the reason why I was getting an error:
to get an array of strings, you need to do

Code:
private String[] a = new String[10];

instead of

Code:
private String a[10];
.
indeed, although its still helpful to provide the runtime error when you want help debugging it.
here's the error:
output wrote:
exception in thread "main" java.lang.NullPointerException
A line number would be incredibly helpful here...
do people listen? Eclipse will tell you where the error is, and ideas on how to fix it.
rivereye wrote:
do people listen? Eclipse will tell you where the error is, and ideas on how to fix it.
Indeed, that's what I started using for my IDE. It doesn't do it for runtime errors, though.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement