Lassativo69
05-02-2011, 15:26
Ciao, ho questo programma
public class BubbleSortCrescente
{static int[]a;
public static void Swap(int i, int j)
{int temp=a[i]; a[i]=a[j]; a[j]=temp;
}
public static void sss(int[]a)
{for (int i=a.length-1; i>=1; i--)
for(int j=0; j<i; j++)
if (a[j]>a[j+1])
Swap (j, j+1);
}
public static void main (String[]args)
{int[]a=new int[10];
a[0]=10; a[1]=6; a[2]=3; a[3]=8; a[4]=2; a[5]=76; a[6]=48; a[7]=8; a[8]=25; a[9]=4;
sss(a);
System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4]+" "+a[5]+" "+a[6]+" "+a[7]+" "+a[8]+" "+a[9]);
}
}
Compilandolo non dà problemi, però quando provo a eseguirlo, nel terminale mi appare questo errore:
Exception in thread "main" java.lang.NullPointerException
at BubbleSortCrescente.Swap(BubbleSortCrescente.java:4)
at BubbleSortCrescente.sss(BubbleSortCrescente.java:11)
at BubbleSortCrescente.main(BubbleSortCrescente.java:17)
Dove sbaglio? :muro: Grazie per l'aiuto :)
public class BubbleSortCrescente
{static int[]a;
public static void Swap(int i, int j)
{int temp=a[i]; a[i]=a[j]; a[j]=temp;
}
public static void sss(int[]a)
{for (int i=a.length-1; i>=1; i--)
for(int j=0; j<i; j++)
if (a[j]>a[j+1])
Swap (j, j+1);
}
public static void main (String[]args)
{int[]a=new int[10];
a[0]=10; a[1]=6; a[2]=3; a[3]=8; a[4]=2; a[5]=76; a[6]=48; a[7]=8; a[8]=25; a[9]=4;
sss(a);
System.out.println(a[0]+" "+a[1]+" "+a[2]+" "+a[3]+" "+a[4]+" "+a[5]+" "+a[6]+" "+a[7]+" "+a[8]+" "+a[9]);
}
}
Compilandolo non dà problemi, però quando provo a eseguirlo, nel terminale mi appare questo errore:
Exception in thread "main" java.lang.NullPointerException
at BubbleSortCrescente.Swap(BubbleSortCrescente.java:4)
at BubbleSortCrescente.sss(BubbleSortCrescente.java:11)
at BubbleSortCrescente.main(BubbleSortCrescente.java:17)
Dove sbaglio? :muro: Grazie per l'aiuto :)