Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a BIG problem with my program. This program is structured with a menu and it has a class for every choise. In the class Vettori I have this problem when I push in the numbers. Sorry for my bad english but I'm Italian.

What I have tried:

this is the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0<br />
	at generale.Vettori.Caricamento(Vettori.java:39)<br />
	at generale.Generale.main(Generale.java:43)<br />
C:\Users\Peppe7\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1<br />
BUILD FAILED (total time: 4 seconds)


this is a class:
Java
package generale;
import java.io.*;

/**
 *
 * @author DE_VITA1700
 */
  public class Vettori {
        //*******************************************
        //Input
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader tastiera = new BufferedReader(input);
        //******************************************* 
     private int N;
     
     public void setN(int x){
        N=x;
     }
     private int V[] = new int[N];
     public void Caricamento() {
         int dato=0;
         String numero;
       
         for(int i=0;i<N;i++) {
            try {
             System.out.print("Valore da inserire alla posizione n°" + i + ": ");
             numero = tastiera.readLine();
             dato = Integer.valueOf(numero).intValue();
            }
            catch(Exception e){
            System.out.println("Inserisci un numero!");
            }
            V[i]=dato;
         }
     }
         public void Stampa(){
         for(int i=0;i<V.length;i++){
             System.out.println(V[i]);
         }
     }
  
  
  
  }
Posted
Updated 14-Oct-18 9:44am
Comments
Richard MacCutchan 14-Oct-18 10:31am    
Where is the code that sets the value of N?
Member 14019184 14-Oct-18 10:50am    
Here you are:

public static void main(String[] args) {
//*******************************************
//Input
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader tastiera = new BufferedReader(input);
//*******************************************

/*Menu m;
m = new Menu();
m.printMenu();
m.voci();*/
Vettori a;
int x;
int valore=0;
String numero;
try {
System.out.print("Grandezza vettore: ");
numero = tastiera.readLine();
valore = Integer.valueOf(numero).intValue();
}
catch(Exception e){
System.out.println("Inserisci un numero!");
}
x = valore;
a = new Vettori();
a.Caricamento(x);
a.Stampa();
Richard MacCutchan 14-Oct-18 11:05am    
There is no code there that calls setN to set the value of N. You are also calling a.Caricamento(x); but Caricamento does not take any parameters.

N is a class level integer, so it gets the default value when the class is instantiated.
V is a class level array, which is initialized when the class is instantiated to an array of N elements.
All of this happens before the class constructor is called, so that the constructor can use the variables.

Since N is defaulted, it's zero, so V is initialized to an array of zero elements. Any attempt to access any element of the array will fail with an "Index out of bounds" error because there are no elements in the array!

Changing N at a later point via your setN method won't cause the array to be re-initialised, you will have to do that yourself as part of the setter.
 
Share this answer
 
Quote:
In the class Vettori I have this problem when I push in the numbers.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900