Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

public class LoadingData
{  
    List <Book> Alist = new ArrayList<>();

    public void loading(String fileName, Class<?> theClass) //read csv file into array list of objects
    {
        Constructor<Book>[] ctors = (Constructor<Book>[]) theClass.getConstructors();
        Constructor<Book> ctor = ctors[0];
        
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) 
        {

            String currentLine;
            boolean first = true;
            while ((currentLine = br.readLine()) != null)//while file is not empty, implement loop
            {
                Object[] details = currentLine.split(",");
               
                if (first) 
                {
                    first = false;
                    continue;
                }
                
                Book instance = ctor.newInstance((Object[]) details);
                //Alist.getClass(instance)= instance.getTitle()title;
                Alist.add(0,instance);     
            }
        }
        catch (IllegalAccessException    |
               IllegalArgumentException  |
               InstantiationException    |
               InvocationTargetException |
               IOException ioe) {
        }
    }
 
    
    public void insertionSort(List<Object> array)
        {
            int i, j;
            for (i=1; i < array.size();i++ )
            {
                String current = (String) array.get(i);
                j=1;
                    while(j>0 && current.compareTo((String) array.get(-1))<0)
                    {
                        array.set(j, array.get(j-1));
                        j--;
                    }
                array.set(j, current);
            }
            System.out.println(array);
        }
    

    
    public static void main(String[] args) 
    {
        //create new object Loading data
        LoadingData Data = new LoadingData();
        
        Data.loading("C:\\Users\\User\\Documents\\NetBeansProjects\\sorting\\src\\sorting\\books.csv", Book.class);

        System.out.println("Before sort");
        System.out.println(Data.Alist);
        System.out.println("After sort");
        insertionSort(Data.Alist);
        
       
            
    }
}
//Book class
public class Book
{

    String bookID;
    String title;
    String authors;
    String average_rating;
    String isbn;
    String isbn13;
    String language_code;
    String num_pages;
    String ratings_count;
    String text_reviews_count;
    String publication_date;
    String publisher;

    public String getBookID() {
        return bookID;
    }

    public void setBookID(String bookID) {
        this.bookID = bookID;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthors() {
        return authors;
    }

    public void setAuthors(String authors) {
        this.authors = authors;
    }

    public String getAverage_rating() {
        return average_rating;
    }

    public void setAverage_rating(String average_rating) {
        this.average_rating = average_rating;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getIsbn13() {
        return isbn13;
    }

    public void setIsbn13(String isbn13) {
        this.isbn13 = isbn13;
    }

    public String getLanguage_code() {
        return language_code;
    }

    public void setLanguage_code(String language_code) {
        this.language_code = language_code;
    }

    public String getNum_pages() {
        return num_pages;
    }

    public void setNum_pages(String num_pages) {
        this.num_pages = num_pages;
    }

    public String getRatings_count() {
        return ratings_count;
    }

    public void setRatings_count(String ratings_count) {
        this.ratings_count = ratings_count;
    }

    public String getText_reviews_count() {
        return text_reviews_count;
    }

    public void setText_reviews_count(String text_reviews_count) {
        this.text_reviews_count = text_reviews_count;
    }

    public String getPublication_date() {
        return publication_date;
    }

    public void setPublication_date(String publication_date) {
        this.publication_date = publication_date;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    
    public Book (String bookID, String title, String authors, String average_rating, String isbn, String isbn13, String language_code, String num_pages, String ratings_count, String text_reviews_count, String publication_date, String publisher ) 
    {
        this.bookID = bookID;
        this.title = title;
        this.authors = authors;
        this.average_rating = average_rating;
        this.isbn = isbn;
        this.isbn13 = isbn13;
        this.language_code = language_code;
        this.num_pages = num_pages;
        this.ratings_count = ratings_count;
        this.text_reviews_count = text_reviews_count;
        this.publication_date = publication_date;
        this.publisher = publisher;
    }

    @Override
    public String toString()
    {
        return String.format(" (%s) %s  %s  %s  %s  %s  %s  %s  %s  %s  %s  %s  \n", bookID, title, authors,average_rating, isbn,  isbn13, language_code, num_pages, ratings_count, text_reviews_count, publication_date, publisher );
    }
}


What I have tried:

i tried to sort this this list of object in insertion sort and it keeps telling me that i have to change methods. can someone help me run this code and tell me what im still doing wrong here
Posted
Updated 21-Sep-22 18:02pm
v2
Comments
Richard MacCutchan 21-Sep-22 7:18am    
Please use the Improve question link above, and add complete details of which line of code is causing a problem, and the exact text of any error messages.
John Bob 2021 21-Sep-22 22:48pm    
The question asked me to read a CSV file into a array list and then sort the file. The error occurs at the main function while calling the insertion sort function, it says non static variable cannot be referenced from a static context
Richard MacCutchan 22-Sep-22 4:45am    
Yes, because insertionSort is a method of the LoadingData class so it should be invoked on an instance of that class:
Data.insertionSort(Data.Alist);

And there is no need to pass it the reference to Data.Alist since Alist is a member of the class also.

1 solution

The error pretty much says it all. Your main function is static. Therefore it can't access instance variables or methods. You are trying to call the instance method insertionSort from the static main method. As the error states this is not valid.

It doesn't appear that insertionSort uses any instance members itself so you could probably just make it static as well.
 
Share this answer
 
Comments
John Bob 2021 22-Sep-22 4:00am    
thank you, the first issue has been resolved, however there's another issue that came up from inside the insertionSort "For loop" and another error is at the main class when calling the insertion sort function. Exception in thread "main" java.lang.ClassCastException: class books.BookList cannot be cast to class java.lang.String (books.BookList is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
FreedMalloc 22-Sep-22 12:03pm    
As you're beginning to see, debugging is a very large part of programming. And a big part of that is interpreting error messages. The error message should tell you exactly the line of code where the error occurred. Look at that line and read the error message.

For the exception in main it should be fairly obvious what is wrong. Are you perhaps passing something that is not a string to a function that requires one? What are you trying to do there? Then you need to write the code to do what you wanted to do in the first place.

You didn't state what the error in insertionSort was. But it's probably something similar. Look at the line, read the message. If you're still stuck use the green Improve Question link to post the exact error message and ask your question.

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