Why got Nullpointer use getJarPath.class.getResource(".").getPath() ?

hi,
I have a question about the following program:
public class getJarPath {
public getJarPath() {
public static void main(String[] arg) {
System.out.println("java.net.URl->" + getJarPath.class.getResource(
"getJarPath.class"));
System.out.println("getPath->" + getJarPath.class.getResource(
"getJarPath.class").getPath());
System.out.println("getFile->" + getJarPath.class.getResource(
"getJarPath.class").getFile());
System.out.println("getPath with . ->"+getJarPath.class.getResource(".").getPath()); // NullPointer occured
1. compile getJarPath.java
D:\>D:\j2sdk1.4.2_04\bin\javac getJarPath.java
2. run this test program with "java getJarPath"
D:\>D:\j2sdk1.4.2_04\bin\java getJarPath
java.net.URl->file:/D:/getJarPath.class
getPath->/D:/getJarPath.class
getFile->/D:/getJarPath.class
getPath with . ->/C:/Program%20Files/exceed.nt/
3. D:\>D:\j2sdk1.4.2_04\bin\jar cvf getJarPath.jar getJarPath.class
4. D:\>D:\j2sdk1.4.2_04\bin\java -cp d:\getJarPath.jar getJarPath
java.net.URl->jar:file:/D:/getJarPath.jar!/getJarPath.class
getPath->file:/D:/getJarPath.jar!/getJarPath.class
getFile->file:/D:/getJarPath.jar!/getJarPath.class
Exception in thread "main" java.lang.NullPointerException
at getJarPath.main(getJarPath.java:15)
question:
in step 2. why the path "." is "/C:/Program%20Files/exceed.nt/" ?
in step 4. why NullPointerException occured?
thank a lot
BR

Your output seems to indicate that all lines executed successfully, yet you say there's a NullPointerException. Which is it?

Similar Messages

  • Why are we using Abstract class?

    Why are we using Abstract class? What is specify use of Abstract class?

    The way I understand it....and I may be wrong because
    I am very new....is that by making the class abstract
    you will add abstract methods to it. Those abstract
    methods MUST be defined in in any subclass that
    inherits from the abstract class thereby making the
    abstract a template for subclasses.
    If your animal class is abstract then you would need
    an abstract method (Which is just siganture, no body)
    of say "numberOfLegs". Then in your dog and cat
    classes which extend animal you must define a
    "numberOfLegs" method with appropriate code.it isn't mandatory for an abstract class to have abstract methods. if a class does have abstract methods, the class must be abstract. but the following is perfectly legal
    public abstract class NoAbstractMethods {
      public void doStuff() {
         // do stuff
    }a subclass of an abstract class can also be abstract, and as such need not implement any additional methods

  • Why JSP not use new class that I compile again?

    I use bean class with JSP. But When I add some code and compile new bean. JSP not use new been class it use old class.
    I try to set file server.xml and set <DefaultContext reloadable="true"/> and restart Tomcat. But it not work?
    any help please?

    I run JSP in http://localhost:8080/stringbean.jsp
    is context name mean localhost
    in c:\tomcat\work has following directory
    standalone (level1 dir)
    localhost (level2 dir)
    - (level3 dir)
    examples (level3 dir)
    manager (level3 dir)
    tomcat-doc (level3 dir)
    webdav (level3 dir)

  • Why should we use SUb Class in Forms - Template

    Whatr is the benefit of using SUbcalss properties in Template.fmb
    Edited by: O.Developer on Oct 29, 2012 12:10 PM

    I'll assume you mean that you want to generate HTTP responses from XML streams using XSL transformations.
    It's a good idea because now you can be flexible about how you render responses. You can have one stylesheet for a browser and another for a WAP phone. You can choose the stylesheet to use based on the kind of client you're talking to. New clients simply mean new stylesheets. Very flexible, very nice.

  • Why use abstract classes?

    Why should I use abstract classes instead of a regular class with empty method bodies? Just better design? Is there some logical or performance based reason?

    Why should I use abstract classes instead of aregular
    class with empty method bodies? Just better design?Is
    there some logical or performance based reason?Because it describes what you're doing.
    If you define a "regular" class with empty message
    bodies, everyone who looks at it will say "WTF is he
    trying to do" or, if they're charitable, "Look,
    Harvey, someone who's trying to make Java look just
    like C++!".
    sigh Maybe answers like THIS are why people keep asking the same question.
    Here's a couple things an abstract class does that a "regular" class with empty methods bodies doesn't:
    1) An abstract class cannot be instantiated.
    2) An abstract class forces it's abstract methods to be implemented.
    If you were to extend a non-abstract class with empty method bodies, you wouldn't have to override the methods... you could just leave them empty. An abstract class forces it.
    There's a lot more reasons... those are a couple obvious ones.

  • Why use Factory classes

    Why do we use Factory classes such BorderFactory, to get an instance of Border class, why cant we directly create an object of such classes like we do with regular concrete classes

    Why do we use Factory classes such BorderFactory, to
    get an instance of Border class, why cant we directly
    create an object of such classes like we do with
    regular concrete classesFactory methods (I mean static create methods here) have two advantages.
    First, the library designer doesn't have to expose concrete classes to the outside world, just interfaces. This adds flexibility.
    Second, the library designer is free to make object creations more flexible. The create method may take parameters that doesn't exactly correspond to a constructor of one specific class.

  • Why Use Nested Classes

    Hi All,
    Why do we use Nested Classes ? Explain it with example ....
    If we say - It is a way of logically grouping classes that are only used in one place. Then we can say we use it through "Package" so why inner class ??
    Thanks in advance ....

    A real life example to jschell's explaination may be this.
    The pistons in your automobile engine are encapsulated by the engine block -- or the engine block encapsulates the pistons inside. This is the desired effect because as soon as we take the pistons out of the engine block, and put them say, in the backseat of the car instead, they no longer perform the function they were intended. There is another side effect to this analogy. When the pistons are properly installed in the engine block, there is only one movement the pistons can move -- up and down movement, that's it... they cannot rotate, spin, twist around or anything inside the engin block. Furthermore, the up and down movement is the only useful thing a piston can do. As soon as we throw the pistons in the backseat of the car, they can roll around, spin, and generally rotate to any configuration or orientation -- which does not produce any useful power.
    Inner classes are generally the same way. So we can see this example with a little code -- which we like.
    public class Engine
      Piston[] pistons = new Piston[8];
      class Piston
        // the usual methods here..
    }Of course, if your program is extremely sohisticated, this may not be a good solution -- for example, if you are making a catalog of automotive parts, then you would probably want your <tt>Piston</tt> class to be stand-alone.
    Edited by: pierrot_2. Also notice that the class <tt>Piston</tt> is not an INNER class of Backseat!

  • How to use class.getResource() to create an ImageIcon

    Hi,
    I am well acquainted with creating and using ImageIcon icons using the ImageIcon constructor
    and putting the image file in a folder called Images which is at the same level as the
    bin and src folders.
    I discovered a demo program, LayeredPaneDemo, that uses class.getResource() to create
    an icon and found that in my eclipse version, the icon's image file was not found when
    I used the original getResource() call but the icon was created when I used the ImageIcon
    constructor.
    I posted on JavaRanch and eventually realized that the image file needed to be with the
    .class files, so I moved the Images folder under bin and getResource() works fine and I'm
    happy.
    However, I have three questions for you.
    One poster on JavaRanch told me that it's better to use getResource() rather than the
    ImageIcon constructor for distributing an app (I'm not distributing anything but want
    to do it all correctly).
    Do you agree with that or can I safe keep using the ImageIcon constructor?
    Another poster told me he doesn't think it's safe to leave the image file in bin because
    it might be lost during the build in eclipse and that there is a way to have eclipse copy
    the files to bin during the build which should mean that I can leave the images folder at
    the level of bin and src.
    Do you agree with that?
    If yes, how do I get eclipse to copy the file during the build?
    P.S.
    Before I posted on JavaRanch, I put the Images folder at every level of the project's
    directory as shown in eclipse (which is why I missed the bin folder until JavaRanch
    whacked me upside the head) and getResource() still didn't work.

    The contents of the Java Source folder are compiled if they're source files, copied otherwise (assuming no filter's been put in place to prevent copying), so your images belong under a source folder.

  • Why do we use only dynamic class loading for JDBC drivers

    Hi,
    My JDBC experience is that we always use dynamic class loader for drivers.
    If we have a well defined package from a vendor, why do we use this dynamic class loading feature for drivers??

    chandunitw wrote:
    Hi,
    My JDBC experience is that we always use dynamic class loader for drivers.
    If we have a well defined package from a vendor, why do we use this dynamic class loading feature for drivers??Oftentimes, the driver class name is set in a configuration file, not in code. So the thing which processes the configuration file has no idea ahead of time which driver or drivers it will support, so it is not coded specifically for any. So it loads the driver by reflection, since it is given the class name as a string it can use with the reflection API.

  • Why to use wrapper class?

    hello,
    can anybody please tell me why wrapper class are used in JAVA and what are exactly wrapper class?
    reply me soon....

    I want to give an example and then explain why it is for. Primitives will be good example for this situation.
    Example:
    Below are the primitive types and their wrapper classes...
    Primitive to Wrapper Class
    byte - Byte
    short - Short
    int - Integer
    long - Long
    char - Character
    float - Float
    double - Double
    boolean - Boolean
    USING OBJECT CREATION
    Think a situation when you create an integer object using this code:
    ---> Integer intVar = new Integer();
    you have the methods and constants:
    1 MAX_VALUE ,
    2 MIN_VALUE ,
    3 byteValue() ,
    4 compareTo(Integer anotherInteger) ,
    5 compareTo(Object o) ,
    6 decode(String nm) ,
    7 doubleValue() ,
    8 equals(Object obj) ,
    9 floatValue() ,
    10 getInteger(String nm) ,
    11 getInteger(String nm, int val) ,
    12 getInteger(String nm, Integer val) ,
    13 hashCode() ,
    14 intValue() ,
    15 longValue() ,
    16 parseInt(String s) ,
    17 parseInt(String s, int radix) ,
    18 shortValue() ,
    19 toBinaryString(int i) ,
    20 toHexString(int i) ,
    21 toOctalString(int i) ,
    22 toString(),
    23 toString(int i) ,
    24 toString(int i, int radix) ,
    25 valueOf(String s) ,
    26 valueOf(String s, int radix)
    now you have an object with 2 constants and 24 methods.
    USING WRAPPER CLASS
    and think using wrapper class of Integer..
    CODE ---> int intVar = Integer.parseInt((String)someStringData);
    In this case your Integer object have the methods and constants
    1 MAX_VALUE ,
    2 MIN_VALUE ,
    3 parseInt(String s) ,
    4 parseInt(String s, int radix) ,
    5 toBinaryString(int i) ,
    6 toHexString(int i) ,
    7 toOctalString(int i) ,
    8 toString(int i) ,
    9 toString(int i, int radix) ,
    10 valueOf(String s) ,
    11 valueOf(String s, int radix)
    as you in this situation you have 2 constants and 9 methods.
    AS YOU SEE THERE ARE 15 METHODS DIFFERENCE FROM THE OBJECT AND THE WRAPPER CLASS OF INTEGER OBJECT.
    NOW TALKING ABOUT PRIMITIVE TYPES ALL PROGRAMMERS USED THIS TYPES FOR THEIR CODES AND THEY ALWAYS NEED SOME THIS KIND OF CASTING. IN MY EXAMPLE I TAKE THE VALUE OF AN STRING VARIABLE. AND THERE ARE LOTS OF THINGS SIMILIAR TO THIS EXAMPLE.
    IDEA OF THIS WRAPPER CLASSES IS THAT USE AS YOU NEED NOTHING MORE. IN THE WRAPPER CLASS YOU DONT HAVE INTEGER OBJECT. YOU ONLY NEED THE PARSEINT METHOD OF INTEGER OBJECT INSTEAD OF CREATING THE INTEGER OBJECT YOU USE THE WRAPPER CLASS OF THIS TYPE...
    OBJECTS ARE STORED IN A MEMORY PLACE CALLED HEAP WHICH IS PLACED ON RAM AND GARBAGING OF THIS OBJECTS TAKES MORE TIME TO ALLOCATE FROM HEAP STORAGE.
    AND ANOTHER NOTE FOR THIS WRAPPER CLASSES if you want to store an int inside a container such as an ArrayList (which takes only Object references), you can wrap your int inside the standard library Integer class
    EX:
    import java.util.*;
    public class ImmutableInteger {
    public static void main(String[] args) {
    List v = new ArrayList();
    for(int i = 0; i < 10; i++)
    v.add(new Integer(i));
    // But how do you change the int inside the Integer?
    } ///:~
    GOOD LUCK...
    REALKINGTA....

  • Why won't -Djava.system.class.loader make it use my ClassLoader?

    I created a class MyAppLoader and installed it in the bootclasspath and used the system property -Djava.system.class.loader. Yet, the JVM won't use my classloader! It's never instantiated (I have a static bit that prints code if it's loaded at all).
    How do I make the JVM use my class as the system classloader?

    send to me some Duke, please Any particular one? I hear the Duke of Northumberland is quite a laugh - would he do?

  • Help using scanner class to count chars in file

    Hi all, I am learning Java right now and ran into a problem. I need to use the Scanner class to accurately count the number of chars in a file including the line feed chars 10&13.
    This what I have and it currently reports a 201 byte file as having 194 chars:
         File file = new File(plainFile);
         Scanner inputFile = new Scanner(file);
         numberOfChars = 0;
         String line;
         //count characters in file
         while (inputFile.hasNextLine())
              line = inputFile.nextLine();
              numberOfChars += line.length();
    I know there are other ways using BufferedReader, etc but I have to use Scanner class to do this.
    Thanks in advance!

    raichle wrote:
    Wow guys, thanks for the help! I've got a RTFMWell, what's wrong to have a look at the API docs for the Scanner class?
    and directions that go against the specs I said.Is that so strange to suggest a better option? What if I ask a carpenter "how do I build a table with a my stapler?". Should I give the man an attitude if he tells me I'd better use a saw and hammer?
    I'm also aware that it "eats" those chars and obviously looking for a way around that.
    I've looked through the java docs, but as I said, I'm new to this and they're difficult to understand. The class I am auditing req's that this be done using Scanner, but I don't see why you demand an explanation.
    Can anybody give me some constructive help?Get lost?

  • Uploading bitmapData using FileReference class ?

    Hi gurus ;)
    Question nr 1.
    Is it possibly to upload bitmapData to server using
    FileReference class ? How should i approach this issue, is there
    any ready classes available for this purpose. I'm generating
    bitmaps inside my Flash/Flex App and need to store them under users
    profile in server.
    Question nr 2.
    Can i upload files from remote server to another using
    FileReference class
    e.g Somehow like :
    uploadURL = new URLRequest();
    uploadURL.url = "
    http://www.[yourDomain
    fileURL.url = "
    http://www.myLocation.com/myfile.JPG";
    file = new FileReference(fileURL.url);
    file.upload(uploadURL);
    This is just an idea, sure not working ;)
    Any ideas are helpful
    Thx
    iquaaani

    I did a small application that uploads a file to the server
    every hour. If the server response that a login is required to
    upload the file, it goes to a login page to do the login and
    retried to upload the file.
    I got the server response in order to know if the file did
    upload correctly or not (I have to send more form data than just
    the file). I didn't try the mime type since it's not relevant for
    my application and usualy this information is not very trust worthy
    anyways.
    I'm not sure what you mean with exceptions, if you are
    referring to HTTP errors, I think there is an event for that, but I
    used the IOError event for this, and it seems to work good
    also.

  • Use of classes Vs tables

    Hi All,
    I have e requirement where i need to select data from tables....... It was told to me that i can make use of classes instead of tables...... I am working on Inbound delivery in EWMmodule.......
    can any one through some light on this....... how can i make use of classes to get data from sap tables.........
    Regards,
    Poonam

    Hi Faisal,
    Just to correct you it's "Methods of Global Persistent Classes".
    Not to hide my ignorance in OOPs, but i don't think persistent classes should be used for mundane selects. It's much easier to write, maintain & troubleshoot a select construct than a persistent class
    You can go through this discussion on: Why do we need Persistent Classes ?
    BR,
    Suhas

  • How can I make server use single class loader for several applications

    I have several web/ejb applications. These applications use some common libraries and should share instances of classes from those libraries.
    But applications are being deployed independently thus packaging all them to EAR is not acceptable.
    I suppose the problem is that each application uses separate class loader.
    How can I make AS use single class loader for a set of applications?
    Different applications depend on different libraries so I need a way that will not share library for all applications on the domain but only for some exact applications.
    When I placed common jar to *%domain%/lib* - all works. But that jar is shared between all applications on the domain.
    When I tried to place common jar to *%domain%/lib/applibs* and specified --libraries* attribute on deploying I got exception
    java.lang.ClassCastException: a.FirstDao cannot be cast to a.FirstDaoHere http://download.oracle.com/docs/cd/E19879-01/820-4336/6nfqd2b1t/index.html I read:
    If multiple applications or modules refer to the same libraries, classes in those libraries are automatically shared.
    This can reduce the memory footprint and allow sharing of static information.Does it mean that classes should be able to be casted ?

    You didn't specify which version of the application server you are using, but the config is similar as long as you know what to look for. Basically, you need to change the classloader delegation. Here's how it is done in 8.2
    http://download.oracle.com/docs/cd/E19830-01/819-4721/beagb/index.html

Maybe you are looking for

  • Regular broadband drop out

    Hi there, I am having trouble with my BT broadband at the moment.  Many times a day it will disconnect and sit there trying to reconnect, sometimes for a minute or two, sometimes for hours. I phoned bt and they went through the generic checklist, and

  • How to get PS to recognize my Epson scanner

    I have PS CS4 for Mac (running Snow Leopard), and am trying to figure out how to Import or scan something into PS.  With my Windows machine I used to use Epson TWAIN from the Import menu.  The only Import options I have on my Mac version are: Video F

  • Set role in a form

    Hi, If I use DBMS_SESSION.SET_ROLE in a form to set a role with password protected, I must put the role password in my form. That what I do not want. Any idea to prevent this from happen? Thanks a lot Stephen

  • Is it possible to delete data selectively from Business content cubes

    Dear Experts,          Requesting you to help me out to know, is it possible to delete data selectively from Business content cubes. When I'm trying to delete selectively from Business content cubes, the background job gets cancelled with ST22 logs s

  • Selecting images in a stack v1.0

    is there a shortcut to 'select all' in a particluar stack, then choose develop module to work on this selection only?