Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to add text fields a01, a02, ... to the array a.
I want to display the value of val so that I would know if the text is being taken from text fields. This code does not show any errors, but, well, it doesn't give me output as well.
int i, j;
JTextField[][] a = new JTextField[9][9];
int[][] val = new int[9][9];
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
val[i][j] = Integer.parseInt(a[i][j].getText());
System.out.println(val[i][j]);
}
}
It is from my old question here.
You did not give them value
int i,j; // counter
JTextField[][] a = new JTextField[9][9];
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
JTextField tf = new JTextField();
tf.setText("a"+i+j);
a[i][j] = tf;
}
}
In your version the call to a[i][j].getText() should throw a NullPointerException. This should either kill your application, end up on the console or you have somewhere something like
try {
// more code here
} catch (Exception ex){}
which will silently swallow the exception and is veeeeery bad practice.
The code you have shown us does generate an error - it throws a NullPointerException when you try to access the text of the text fields via a[i][j].getText().
You didn't initialize the JTextFields in your array a.
ideone example
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Had a problem with compilation, getting error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at practise.code.main(code.java:11)
Here is my code:
package practise;
public class code {
static int number[]={1,8,5,9,4,7};
static int c[] = new int[number.length];
static int p=0;
static int q;
public static void main(String[] args){
change(number);
System.out.println("Array Before Bubble Sort");
for(int y: c){
System.out.print(y + "\t");
}
}
public static void change (int x[]){
for(int a: x){
for(int i=0; i<=x.length; i++){
if(a > x[i]){
continue;}
else {
p++;}
q = x.length - p - 1;
c[q] = a;
}
}
}
}
}
Some meta-help for future reference:
Compiler errors: these are when you experience an error that occurs at compile time. For Java, this is when you use javac, and your Java code is being turned into bytecode files for interpretation later.
Runtime errors: this is when you experience an error that occurs when you run your code. For Java, this is when you use java, and your Java code is being run.
If you find the two confusing, add into your question the thing you typed in order to experience the error. Specify all the flags and options you used, and format it with a code block, for example like this:
java -jar code.jar
When asking questions here - or indeed anywhere on the web where you can get technical help - try to ask yourself what clarifications you would need if you saw your question for the first time. Your first edit did not include your code, so ask yourself: would you be able to ascertain someone else's similar problem without code? Broadly here the answer is "no", and thus the moral of this story is: always include your code.
Also, do spend a moment to learn the code formatting tools. To use them, paste your block of code into the question, select it, and click the "code" button. It will apply a four-space Markdown indent, which you can now see in the question.
If you need to add clarifications to your post, it is OK to add them as comments, but do also edit the body of the question so that new readers can understand the question. It is well worth spending time making it as readable and clear as possible, so you can get the best possible help, and so that people do not take a look and decide that another question is a better use of their time.
Since you are using an IDE, do you get any warnings/errors in the editor, to help you identify potential problems in you code? If so, and you do not understand them, then paste them into your question, in order to clarify it.
Thanks for your advices... Finally made it working..
thank you once again
package practise;
public class code{
public static void main(String[] args){
int[] Array = {5,8,6,4};
int[] newArray = new int[Array.length];
int a, b, c, d, e, f =1;
for(int z : Array ){
d=0;
for(int i=0; i<Array.length; i++){
a = z; b = Array[i];
if( a >= b){
continue;}
else{
d++;}
}
c = Math.subtractExact(Array.length , d);
e = Math.subtractExact(c, f);
for(int j=0; j< Array.length; j++){
while( j == e){
newArray[j] = z;
break;
}
}
}
System.out.println("Here is your old Array :");
for(int k : Array){
System.out.println(k);
System.out.println("Here is your new Bubble Sort Array :");
for(int q : newArray){
System.out.println(q);
}
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm not exactly new to programming, but for some reason I cannot get past this issue. I'm writing a method, and keep getting the "reached end of file while parsing" compiler error. Normally this happens when you forget a }, but this method only has two sets of brackets, and I'm not missing either close. Could anyone point out why I'm getting this error?
public class Locations{
//member variables
static int totalNumberOfRooms = 0;
int numberOfExits;
//pointers to each exit
String roomGeneralDescription;
String roomDescription;
//member methods
String getRoomGeneralDescription(){
return this.roomGeneralDescription;
}
String getRoomDescription(){
return this.roomDescription;
}
//constructor to more easily create objects
public Locations(int exit, String description, String generalDescription){
totalNumberOfRooms += 1;
numberOfExits = exit;
roomDescription = description;
roomGeneralDescription = generalDescription;
}
//default constuctor
public Locations(){
totalNumberOfRooms += 1;
}
//generates the given number of Locations obejcts, with pointers stored in a returned
//array.
Locations[] createLocations(int x){
int iterate = 1;
int loopMax = x;
Locations[] arrayOfLocations = new Locations[x -1];
while (iterate <= loopMax){
int index = iterate -1;
arrayOfLocations[index] = new Locations();
iterate += 1;
}
return arrayOfLocations;
}
You are missing a closing brace } at the end of your file.
Locations[] createLocations(int x){
int iterate = 1;
int loopMax = x;
Locations[] arrayOfLocations = new Locations[x -1];
while (iterate <= loopMax){
int index = iterate -1;
arrayOfLocations[index] = new Locations();
iterate += 1;
}
return arrayOfLocations;
}
} // YOU NEED TO ADD A CLOSING BRACE TO FINALIZE THE CLASS DEFINITION
Update: Even though the closing brace was the solution, I couldn't help but notice at how your createLocations method is actually written. It's an odd way to do an array allocation. I'm not even sure what you have will run without crashing because the array is allocated to be of size [x-1]. In any case, here's a more clean solution to creating an array in Java. I hope this helps!
Locations [] createLocations(int count) {
Locations [] arrayOfLocations = new Locations[count];
for (int i = 0; i < count; i++) {
arrayOfLocations[i] = new Locations();
}
return arrayOfLocations;
}
} // YOU NEED TO ADD A CLOSING BRACE TO FINALIZE THE CLASS DEFINITION
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
It seems to be a very common question relating with arrays and comparing in java, however I couldn't find the right answer in all of these for my case.
In this application I am trying to make a program which 'encrypts' text given by the user. For example: user gives characters 'a b c' and the program returns it as '# # $'. But as you may notice I am having some issues in the code
"pozita[i] = j;".
Why won't this code work? It doesn't give me an error? Or is there anyway of doing it as "new pozita[i]" or anything like that?
Well, I'd be glad if someone could help me through. I am stuck for a while. Thanks in advance! :)
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class TestPerProgram extends JFrame
{
char[] alfabeti = {'a','b','c','r','n','t'};
char[] kodimi = {'#','#','%','*','^','$'};
int[] pozita;
//Scanner merr = new Scanner(System.in);
String fn = JOptionPane.showInputDialog("Jepe tekstin:");
char[] input = fn.toCharArray();
void numro()
{
for (int i=0; i<=input.length; i++)
{
for(int j=0; j<=input.length; j++)
{
if(alfabeti[j] == input[i])
{
pozita[i] = j;
System.out.println(pozita[i]);
}
}
}
/*
for (int k=0; k<=input.length; k++)
{
System.out.println(pozita[k]);
}
*/
}
public static void main(String[] args)
{
TestPerProgram pjesa = new TestPerProgram();
pjesa.numro();
}
}
I'm not 100% clear on how your algorithm is supposed to work, but it seems you may want to replace the line
pozita[i] = j;
with
pozita[i] = kodimi[j];
Right now, you are only writing the matching index to pozita, not a replacement character.
If my assumption is correct, you would also change
int[] pozita;
to
char[] pozita;
and initialize it to an array of length input.Length.
You never instantiated the array pozita. After you instantiate pozita, you can then start overriding the values in pozita. You are assigning j to posita[i] , posita is null.
Do something like:
int posita[] = new int[20]
and if you don't want to set size then just use an arraylist.
You have not requested memory to be allocated for your variable pozita or otherwise instantiated it. The way you are currently using it, you would write pozita[] = new int[input.length]; at some point after retrieving your input from the user.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Can someone help me understand where the out of bounds exeption is happening. The compiler said its here if(content[i] == delSub[j]) and out of bounds by 5. is that really where its happening and if so why?
char[] content = new char []{'A','B','C','D','E','F','G'};
//SubString = BCDEF
public int deleteSubString(String delSubString)
{
int count = 0;
char[] delSub = new char[delSubString.length()];
String temp = "";
for(int i = 0; i < content.length;i++)
{
for (int j = 0; i < delSub.length;j++)
{
if(content[i] == delSub[j])
{
temp = temp + content[i];
}
}
}
return 0;
//TODO configure return statement conditions
}
You're probably running out of the boundries of delSub[j] - since your condition in the inner loop checks the value of i instead of j
for (int j = 0; i < delSub.length;j++)
Should be:
for (int j = 0; j < delSub.length;j++)
(note the "j <" instead of "i <")
I guess you need to use j for the for condition match
for (int j = 0; i < delSub.length;j++)
I'm going to address just this part of your Question.
... is that really where its happening ...
Yes. It is. (See other answers for an explanation ...)
But more to the point, there is no good reason to doubt that that is where it is happening unless:
you have stuffed up, and the source code you are looking at doesn't match the code that you are running, or
there is a JVM bug that causes it to get the line number incorrect. And there isn't. Sun / Oracle JVMs give reliable line numbers in stack traces... for as long as I can remember.
In short - trust the evidence in a stacktrace.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm doing problem 10003 "cutting sticks" of the UVa online judge, I'm pretty sure my code works and I think I' correctly exiting the problem. But still I get a runtime error, I read somewhere that is because I'm not exiting the application like I'm supposed to do it. I wish you guys can help me with this problem.
import java.io.*;
class MAIN {
static int len, c, min;
static int mat[][] = new int[52][52];
static int arr[] = new int [52];
static BufferedReader BufferReader = new BufferedReader(new InputStreamReader(System.in));
public static void llenaarr()throws IOException{
for(int i=0; i<c-1; i++) {
arr[i+1] = Integer.parseInt(BufferReader.readLine());
}
arr[0] = 0;
arr[c] = len;
}
public static void llenamat(){
for(int i=0; i<=c; i++) {
for(int j=0;j<=c;j++) {
mat[i][j] = Integer.MAX_VALUE;
}
}
for(int i=0; i<c; i++) {
mat[i][i] = 0;
mat[i][i+1] = 0;
}
mat[c][c] = 0;
for(int i=0; i<c-1; i++) {
mat[i][i+2] = arr[i+2] - arr[i];
}
}
public static void minimo(){
for(int k=3; k<=c; k++) {
for(int i=0; i<=c-k; i++) {
for(int j=i+1; j<=i+k-1; j++) {
min=mat[i][j] + mat[j][i+k] + arr[i+k] - arr[i];
if((min< mat[i][i+k])) {
mat[i][i+k] = min;
}
}
}
}
}
public static void main (String args[]) throws IOException {
while((len = Integer.parseInt(BufferReader.readLine())) > 0){
c = (Integer.parseInt(BufferReader.readLine()))+1;
llenaarr();
llenamat();
minimo();
System.out.println("The minimum cutting is "+mat[0][c]+".");
}
}
}
Your program is throwing some kind of runtime Exception during processing input. To verify this, you should encapsulate your code (the part that operates on the input and produces output) in a try-catch block.
The first thing to try to catch is the generic Exception. This will catch any type of runtime exception. Run it through the online judge again, and you should now see a Wrong Answer result (assuming something is not processing right, because it is throwing an exception after all).
Now comes the detective work. Go back to your source code and look at all the calls you make and use the API Docs to check all the possible exceptions they may throw. For example, if you call Integer.parseInt(inputString), it may be throwing a NumberFormatException. So above your catch (Exception e) block, start adding some more specific exceptions you think may be causing the problem.
Ideally (if you have the time and patience) you'll want to add these one at a time and resubmit to the online judge after each one. When the result moves from Wrong Answer back to Runtime Error, you'll know you've found the exception that is causing the problem. From here, hopefully this will help you narrow down what to focus on (to fix) in your source code solution.
Good luck.