-1

I am importing a Jar file for use of its classes (or .java's) into NetBeans IDE. The Jar is pircbot.jar. The inside class I want to call is PircBot. I imported like this:

import org.jibble.pircbot.*;

And I called them like this:

PircBot.connect("irc.twitch.tv", 6667, passwordInput);

Now if the .jar is in my libraries section of NetBeans IDE, and I imported it correctly: why and I getting this error:

non-static method connect(String, int, String) cannot be referenced from a static context

All the Jar errors are from imports, and all the static context problems are people calling from the main() class.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
DaCoder
  • 83
  • 1
  • 8
  • Not exactly: my problem was from PircBot being abstract, it was a specific question: not a general one. – DaCoder Jan 21 '14 at 03:06
  • I'd say that's a different error. The problem here is that you need an instance. The problem with PircBot being abstract doesn't arise until you try to create one. – Joshua Taylor Jan 21 '14 at 03:07
  • So now I am banned from asking question on StackOverflow? -_- – DaCoder Jan 21 '14 at 08:52
  • I'd be a bit surprised if just one question with downvotes would invoke a question ban (I know that there is a question rate limiting, but I don't know the details of that). If you do have a question ban, you can ask about it on http://meta.stackoverflow.com/. – Joshua Taylor Jan 21 '14 at 13:15
  • But as a note about the close vote: I do understand that the eventual solution that _you_ need is to find a factory for PircBots, or to implement one, or something that will provide you an instance, and that's going to be the main challenge. However, what actually appears in the question is "non-static method … cannot be referenced from a static context." Future users who need information about abstract classes won't find this question by searching, but users who need information about static vs. non static contexts will, and their question has already been answered. – Joshua Taylor Jan 21 '14 at 13:19
  • But as to the downvotes, the tooltip on the downvote button says "This question does not show any research effort; it is unclear or not useful." The error message that you provided is [pretty easy to search for](https://www.google.com/search?q=non+static+method+cannot+be+referenced+from+static+contenxt&oq=non+static+method+cannot+be+referenced+from+static+contenxt), and the first few hits are even StackOverflow questions. A bit of research should have transformed this question into "How can I create an instance of PircBot, since it's abstact?" and then this question would be on-topic. – Joshua Taylor Jan 21 '14 at 13:22
  • @JoshuaTaylor My issue was that I could find nothing on .jar's + imports + abstract issues. All the stackoverflow questions were precise answers that I could not benefit from. – DaCoder Jan 22 '14 at 03:04

2 Answers2

3

As your error message states, connect isn't a static method and requires an instance to call it on. This has nothing to do with jar files or imports and all to do with basic nuts and bolts Java. Read up on classes and creating instances or objects of classes. Bottom line, you first create a PircBot instance, and then call the method on it.

So I hope no one will do what people in IRC chat do: "ugh how can you not know this?" or "that has been asked a hundred times, go look it up" Because I have looked it up, everywhere I could.

I can't criticize you for not knowing Java as none of us were born with this knowledge, but I would suggest that you work on your Google skills as this question has been asked on this site, more then five times a day, and the information is easy to find if you look.


Edit
You state:

Unfortunately: I have tried this: I get "abstract method cannot be instantiated" Any knowledge of this?

You've more research to do. PircBot is abstract class meaning that you can't simply call a PircBot constructor and get an instance. Instead you will need to find a concrete child class of the PircBot class and use an instance of that class. Hopefully the jar file has some documentation that will help you find this. You may need to call a factory method to achieve this. To find this, see if PircBot has any static methods that return PircBot objects.


Edit 2
On review of the documentation, I'm wrong: you have to create the concrete class that extends PircBot. In your new class you will have to implement any and all abstract methods. You should go through the tutorial that they supply. Why stumble when you've got good documentation?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

The class that you are wanting to use is not a static class, therefore you need to instantiate the class first as in

PircBot bot = new PircBot();

I am not familiar with this class, so the constructor maybe be different from above.

here is a link to the API

It looks like you need to implement it yourself.

import org.jibble.pircbot.*;

public class MyBot extends PircBot {

    public MyBot() {
        this.setName("MyBot");
    }

}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64