two doubts about arrays in java


I tell you, I have this class on one side

static class Item implements Serializable {
    int Id;
    String nombre, descripcion;
    int tipo; //{1-armas, 2-armadura, 3-miscelánea}
    int experiencia;
} //fin clase Item

I also have a method where I fill an array with Potions of this class, for example PocimaVida25 ... I have to show this array, when I put this code:

System.out.println("Tus ítems son los siguientes: ");
    for(int j = 0; j <= listaItems.length; j++) {
        System.out.println(j + 1 + ": " + listaItems[j].nombre);

I get this appears, that is, I shows them well but then tells me that points to a null site, and I do not know how to fix it:

  Tus ítems son los siguientes: 
1: Espadas de Kratos
2: Arco de Diana
3: Escudo de la Gorgona
Exception in thread "main" java.lang.NullPointerException

My other doubt is that in another exercise I have to make it appear alphabetically, and in this I do not know how to do it, I read on the internet that you can use an instruction called sort, but that has not been taught to us in class so I do not think I can use it: At the moment I have this, but I do not know how to go about comparing it, or I'm even going well:

public static void MostarItemAlfabeticamente(Item [] listaItems, int MAXITEMS, Item golem, Item esqueleto, Item kratos, Item diana,Item sabiduria, Item gorgona ){
         for(int i = 0;i < listaItems.length;i++){
             char primeraLetra = listaItems[i].nombre.charAt(1);



asked by Jorge Alcañiz Roda 15.12.2018 в 11:32

2 answers


For the NullPointerException (NPE) set within the listaItems that you must have some position null . The array acts as a drawer to save items , but if in a drawer you lack a item (it's null ), you can not call methods on what there is not.

Depending on how you want to manage, you can choose not to have empty positions ( null ) or, before calling a method on item , verify that it is not null .

Regarding the ordering, if you have been taught some method you should do it that way.

A basic algorithm for ordering is that of the bubble if you want to google it to see how it works.

Using java apis you can order a Array with Arrays.sort() . In this case Item must implement Comparable<Item> and you have to define in Item the method compareTo(Item) to return -1, 0, or 1 depending on whether the item compared is less, equal, or higher than item passed by parameter.

answered by 15.12.2018 в 15:28

The NullPointer gives you because as you put it when starting j, the index of collections / array in Java starts with 0, so its maximum index is length -1. Leave the exit condition a minor that (without equal) and it will work for you. It also removes the j + 1 or the last element will also go over the range.

for(int j = 0; j < listaItems.length; j++) {
    System.out.println(j + ": " + listaItems[j].nombre);

The java.util.Collections class allows you to sort lists, even if they contain Classes you can provide it with a java.util.Comparator in which you indicate how it should be sorted. According to your example you should create a Comparator

static class ItemComparator implements Comparator<Item> {
    public int compare(final Item o1, final Item o2) {
        return (o1.nombre.compareTo(o2.nombre));

And in your method, pass the Array that arrives to you list and call Collction.sort passing it the Comparator (in the example I leave it with Lambdas if you use Java 8 so you can see how to save the previous Class)

public static void MostarItemAlfabeticamente(final Item[] listaItems) {
    final List<Item> items = Arrays.asList(listaItems);
    final Comparator<Item> comp = (o1, o2) -> (o1.nombre.compareTo(o2.nombre));
    Collections.sort(items, comp); // Aquí ya tienes la lista ordenada
    for (Item it: items) {
        // Mostrar datos item

Finally, I recommend that if you are going to manipulate collections, use Collection (List or Set, using Set to keep duplicates) instead of Arrays

answered by 15.12.2018 в 15:38