2

I am trying to implement a vector of generic data type where the type is taken as input from the user such as :

package com.example.genericvector;

import java.util.Scanner;


public class GenericVector <generic_type>{

    private int length;
    private generic_type [] vector;

    GenericVector(generic_type element){
        this.vector[0] = element;
    }


    public void allocate_size(int length){

    }
    public void push_back(generic_type element){

    }
    public void display(generic_type [] vector){

        for(generic_type element : vector)
            System.out.print(element);

        System.out.println();

    }

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the data type of vector : ");
        String vector_type = scanner.next();

        switch (vector_type){
            case "integer":
                GenericVector <Integer> vector_int = new GenericVector <Integer>(new Integer(10));
                break;
            case "double":
                GenericVector <Double> vector_double = new GenericVector <Double>(new Double(10.57));
                break;
            default:
                System.out.print("Invalid data type");
        }
    }
}

Upon running the above code I get the following as output:

    Enter the data type of vector : integer
Exception in thread "main" java.lang.NullPointerException
    at com.example.genericvector.GenericVector.<init>(GenericVector.java:12)
    at com.example.genericvector.GenericVector.main(GenericVector.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

I am pretty new to Java, so any help will be much appreciated.

Joshua Goldberg
  • 4,107
  • 1
  • 26
  • 38
Abhijeet Mohanty
  • 304
  • 1
  • 4
  • 16
  • Just for the record - in case you found any of the answers helpful, you can still accept it; gives a little push to your reputation too. – GhostCat Nov 09 '16 at 20:20

2 Answers2

1
private generic_type [] vector;

only declares a field (array) of your generic type. But initially, the array is null.

Something like

private generic_type [] vector = new generic_type[10];

initializes that field with a new vector (but please note that the individual array slots are all null at this point, too). But, uups, commentors are right ... that is one of the deficiencies of generics and arrays, you can't use new[] for generic types. On the other hand, arrays have a fixed size in Java; so you would have to constantly check if you have to grow/shrink that underlying array! Thus a working solution could be:

private List<T> vector = new ArrayList<>();

for example (using the existing Java collection classes here).

Final note: by convention, generic types are denoted using single upperchase characters. So it should be GenericVector<T> instead of generic_type.

GhostCat
  • 127,190
  • 21
  • 146
  • 218
0

You didn't intialize the array vector and thus this.vector[0] will throw a NullPointerException.

Have a look here on how to create a generic array instance:How to create a generic array in Java?

Edit: your constructor or allocate_size() method would then contain something like this:

vector = (generic_type[]) Array.newInstance(element.getClass(), length);

Also note that you need to provide a length for your array so in the constructor you're calling the array must be initialized to have at least length 1, otherwise vector[0] will result in an ArrayIndexOutOfBoundsException.

Community
  • 1
  • 1
Thomas
  • 80,843
  • 12
  • 111
  • 143