0

I'd like to populate an array with Vectors. The variable is declared as below:

private List<Object[]>[] group; 

However, when I populate it with a Vector, below exception is thrown:

java.lang.ArrayStoreException: java.util.Vector
    at presentationtier.GraphMB.createTables(GraphMB.java:68)
    at presentationtier.GraphMB.init(GraphMB.java:48)

How is this caused and how can I solve it?

public List<Object[]> getInv_AccountingError(String action){
        EntityManagerFactory emf = null;
        EntityManager em = null;
        List<Object[]> list = new ArrayList<Object[]>();
        try{
            emf = Persistence.createEntityManagerFactory("Test");
            em = emf.createEntityManager();

            Query query = em.createNativeQuery("select k.* from "+action+" k");
            list = query.getResultList();   

I am getting a List of object array from the db and i loop around this method like call it n times to get a array of list then why vector is comming?

  • 1
    Why are you hiding the code far right? Is it too embarrassing? – BalusC Feb 10 '16 at 08:58
  • No , i copied it directly from my xhtml file and i think its good not embarrassing :) –  Feb 10 '16 at 09:05
  • It will be great if you helped Balus –  Feb 10 '16 at 09:23
  • You didn't tell which error you got. A good error is already the whole answer at its own. If you're not able to interpret it, then you should not ignore it as if it's irrelevant information. Instead, share it with us so that we can translate the error message in layman's terms. At least, my educated guess is that you actually need instead of . – BalusC Feb 10 '16 at 09:27
  • SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/Dashboarddetails] threw exception [An error occurred performing resource injection on managed bean test] with root cause java.lang.ArrayStoreException: java.util.Vector the page is not loading it self its crashing –  Feb 10 '16 at 09:46
  • javax.servlet.ServletException: An error occurred performing resource injection on managed bean test javax.faces.webapp.FacesServlet.service(FacesServlet.java:659) –  Feb 10 '16 at 09:50
  • You got a whole exception and stack trace? That's really valuable information. The entire answer is right in there. Copypaste it in your question in a code formatted block. In the meanwhile, have you tried googling/reading about `java.lang.ArrayStoreException`? First port of call would be just checking its javadoc to learn what exactly it means: https://docs.oracle.com/javase/8/docs/api/java/lang/ArrayStoreException.html – BalusC Feb 10 '16 at 09:55
  • Yes i have donot know why it is given this error all my methods are returning the proper values. –  Feb 10 '16 at 09:57
  • The exception clearly says it doesn't :) – BalusC Feb 10 '16 at 09:58
  • When i am running a dummy method it is populating but in the array of lists the error is comming what to do –  Feb 10 '16 at 10:09
  • Do what I asked. Help us to help you. Copypaste the exception in your question in a code formatted block. Only then we can fully translate it for you in layman's terms. – BalusC Feb 10 '16 at 10:12
  • copy pasted the error in console –  Feb 10 '16 at 10:18
  • The problem is how you instantiated the `group` variable. Apparently it's not a `new Vector[n]` while the code is expecting that. I've in the meanwhile cleaned up noise from your question to put a better focus on the real problem. It's at least not a JSF related problem anymore but just basic Java (as the root exception is of `java.lang.*` package and is coming from own code). – BalusC Feb 10 '16 at 10:28
  • Wow @BalusC, "cleaned up" is quite the understatement. But you're right, it's clearly "just basic Java". – Erick G. Hagstrom Feb 10 '16 at 10:51
  • Can u help me on this i m not able to figure this out –  Feb 10 '16 at 12:46
  • What i am trying to do id make a array of object array type list. Because i expect to get values of object array type and there are n no of such arrays –  Feb 10 '16 at 12:59
  • Hi Balus i have edited my question please suggest something ur help is required , not able to figure this one –  Feb 12 '16 at 09:25

3 Answers3

0

The error (Throws ArrayStoreException) occurs in the following situation in your case.

If a component of this list is not of a runtime type that can be stored in the specified vector.

vineeth sivan
  • 510
  • 3
  • 18
  • refer [link](https://docs.oracle.com/javase/7/docs/api/index.html?java/lang/ArrayStoreException.html) for more details. – vineeth sivan Feb 10 '16 at 10:44
  • i have edited my question please suggest something ur help is required , not able to figure this one –  Feb 12 '16 at 09:25
0

Your group variable is of a rather complicated type. Each element of group has to be of type List<Object[]>. The ArrayStoreException occurs when you try to put the wrong type of element into an array. So there are only two possibilities here:

  1. You're trying to insert into group an element that isn't a List that contains an array of Object; or
  2. You're trying to populate one of those arrays of Object, but you declared it as something more specific than Object[] and now you're trying to put the wrong type into it.

Based on your comments, you probably need to define group like this:

private Object[][] group;

This makes an array of Object arrays. So when you get a value of type Object[], you'll be able to store that as an element of group:

Object[] anElement = // well, wherever they come from
group[i] = anElement;
Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • Can u help me on this i m not able to figure this out –  Feb 10 '16 at 12:46
  • What i am trying to do id make a array of object array type list. Because i expect to get values of object array type and there are n no of such arrays –  Feb 10 '16 at 12:59
  • i cannot define the group that way as i am getting a list from the method.i have shown my method body in your description –  Feb 12 '16 at 07:25
  • i have edited my question please suggest something ur help is required , not able to figure this one –  Feb 12 '16 at 09:26
0

In your original version of the post you seem to have two variables named 'var'.

First one in the for-each tag that has integer-indexes and another for the data-table tag.

My guess is that when you attempted to access the 'columns' element with the value of the 'var' it was not the one with the integer value but the one with the array!

Ravindra HV
  • 2,558
  • 1
  • 17
  • 26
  • i have edited my question please suggest something ur help is required , not able to figure this one –  Feb 12 '16 at 09:26
  • To answer why the `query.getResultList()` is returning a vector is because [a vector is a list as well](https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html). [This](http://stackoverflow.com/questions/23202730/typedqueryx-returns-vector-of-object-instead-of-list-of-x-type-object) or [this](http://stackoverflow.com/questions/13700565/jpa-query-getresultlist-use-in-a-generic-way) post may be more helpful to you – Ravindra HV Feb 12 '16 at 19:57
  • So , what to do i have aready made a array of list why its not working then –  Feb 13 '16 at 08:05
  • Can you just tell me how to make a datatable of a list in a list like- List> group = new ArrayList>(); –  Feb 13 '16 at 10:44
  • I believe it will look something like this : `List> listOfListOfObjectArray=null; for(List listOfObjectArray : listOfListOfObjectArray) { Object[] rowData = listOfObjectArray.get(0); for(int i=0;i – Ravindra HV Feb 13 '16 at 18:05
  • Also [see if this link helps](http://stackoverflow.com/questions/13246538/jsf-display-database-records-on-datatable) – Ravindra HV Feb 13 '16 at 18:38
  • This is not a datatable in jsf or primefaces form its in a console manner i want it in HTML way –  Feb 13 '16 at 19:36
  • Sorry for the late reply. First try to do it in the console (in the dao itself to test, before iterating in the jsp). I have already provided a snippet how that could look. Once you know how to do that, then you need to use EL and JSTL tags. There is `c:foreach` and `c:if` in JSTL which should be helpful to you. – Ravindra HV Feb 14 '16 at 18:46