-1
public class Bank {


public static void nsUI(){
    Bank objBank = new Bank();
    objBank.LoadData();
}



public void UI(){


    if(UserInput==7){
        Exit();

    }

}






public void Exit(){
System.out.printf("Have a Wonderful Day! %n");
IntSLList.printAll();   /////////This is where the problem is
//  WriteData();
//System.exit(0);
}



public static void main(String[] args) {
    nsUI();
 }


    }








**This is the Linked list file**

//************************  IntSLList.java  **************************
//           singly-linked list class to store integers

public class IntSLList {
protected IntSLLNode head, tail;
public static void createInst(){
    new IntSLList().printAll();

}



public IntSLList() {
    head = tail = null;
}
public boolean isEmpty() {
    return head == null;
}
public void addToHead(String AN) {
    head = new IntSLLNode(AN,head);
    if (tail == null) // checks for empty list
        tail = head;
}
public void addToTail(String AN, Double AB) {
    if (!isEmpty()) {
        tail.next = new IntSLLNode(AN,AB);
        tail = tail.next;
    }
    else 
        head = tail = new IntSLLNode(AN,AB);
}
public String deleteFromHead() { // delete the head and return its info; 
    String AN = head.AccountNumber;
    if (head == tail)    // if only one node on the list;
         head = tail = null;
    else head = head.next;
    return AN;
}
public String deleteFromTail() { // delete the tail and return its info;
    String AN = tail.AccountNumber;
    if (head == tail)    // if only one node in the list;
         head = tail = null;
    else {               // if more than one node in the list,
         IntSLLNode tmp;    // find the predecessor of tail;
         for (tmp = head; tmp.next != tail; tmp = tmp.next);
         tail = tmp;     // the predecessor of tail becomes tail;
         tail.next = null;
    }
    return AN;
}
public void printAll() {
    for (IntSLLNode tmp = head; tmp != null; tmp = tmp.next)
        System.out.print(tmp.AccountNumber + " " +tmp.AccountBalance+"");                
}
public boolean isInList(String AN) {
    IntSLLNode tmp;
    for (tmp = head; tmp != null && tmp.AccountNumber != AN; tmp = tmp.next);
    return tmp != null;
}
public void delete(String AN){  // delete the node with an element el;
    if (!isEmpty())
        if (head == tail && AN == head.AccountNumber) // if only one
             head = tail = null;             // node on the list;
        else if (AN == head.AccountNumber) // if more than one node on the list;
             head = head.next;    // and el is in the head node;
        else {                    // if more than one node in the list
             IntSLLNode pred, tmp;   // and el is in a non-head node;
             for (pred = head, tmp = head.next;  
                  tmp != null && tmp.AccountNumber != AN; 
                  pred = pred.next, tmp = tmp.next);
             if (tmp != null) {   // if el was found;
                 pred.next = tmp.next;
                 if (tmp == tail) // if el is in the last node;
                    tail = pred;
             }
        }
    }
}

Eclipse is yelling at me saying that I can't make a static reference to a non-static method, but how should I go about doing that... I read that I need to make an instance of the IntSLL class, and I tried that, but I still can't seem to get it to work... I think I'm doing it wrong.

PS, please don't grill me for the formatting of this post. Its the first time I've ever posted here, usually I can figure out my problems based on people having similar issues, and the awesome community that is here giving different ideas.

PPS, I know that I don't need the printAll method in the method of Exit(), I'm trying to figure out what I need to do to get this to work through my program step by step to implement the linked list rather than the linear search... Eventually the linear search will be replaced by the the implementation of the linked list.

Thanks everyone!

UberPwnd
  • 5
  • 5
  • tl;dr - just post the relevant part of your code. – Scary Wombat May 28 '14 at 05:42
  • Sorry about that, I shortened it as best I could. I realize that was a lot to sift through for one little thing. – UberPwnd May 28 '14 at 05:50
  • basically what it is telling you, is that from you static main, you needs to either call static methods or create an object and call methods on the object. – Scary Wombat May 28 '14 at 05:53
  • possible duplicate of [calling non-static method in static method in Java](http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java) – Patricia Shanahan May 28 '14 at 06:11
  • The reason is `IntSLList.printAll();`, IntSLList is your class while not instance, you can only use ClassName.method to call those static method. For non-static method, the instance should exist before calling that method. – Robin May 28 '14 at 06:34
  • I figured it out! I added public void createInst(){ new IntSLList().printAll(); } And made printAll static, as well as declared the field "head" static! – UberPwnd May 28 '14 at 06:36

1 Answers1

0

Well first off you are calling a IntSLList method in your bank class, but you never created a list. I think that you need to create an instance of a list in your bank class. For example:

public class Bank {
private myList = new IntSLList();
//rest of your code
}

Then, instead of calling IntSLList.printAll() you call myList.printAll(). I hope this makes sense, I may have interpreted your implementation incorrectly and apologize if I did, but I believe this is where your issue is. If I am correct then you should also be adding data into your list within your bank class. Hope this helps! Good luck!

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
andyb2793
  • 32
  • 5
  • I figured it out! I added public void createInst(){ new IntSLList().printAll(); } And made printAll static, as well as declared the field "head" static! – UberPwnd May 28 '14 at 06:51
  • I created the node, I just didn't include it. Its in its own seperate class as my assignment called for. – UberPwnd May 28 '14 at 07:11