FAQ
Hello everybody !



This exception was thrown when I tried to copy a file from local file to
HDFS.

This is my program :

***************************************************************

import api.InterfaceAudience;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.awt.event.ActionEvent;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.*;

import org.apache.hadoop.conf.Configured;

import org.apache.hadoop.io.IOUtils;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.fs.FSDataOutputStream;



@InterfaceAudience.Private

public class CopyFile extends Configured {



private JFrame form;

private JTextField box1;

private JTextField box2;

private JButton sobr;

private JButton debr;

private JButton copy;

protected FileSystem fs;





public CopyFile() {

init();

}



private void copyFromStdin(Path dst, FileSystem dstFs) throws IOException
{

if (dstFs.isDirectory(dst)) {

throw new IOException("When source is stdin, destination must be a
file.");

}

if (dstFs.exists(dst)) {

throw new IOException("Target " + dst.toString() + " already
exists.");

}

FSDataOutputStream out = dstFs.create(dst);

try {

IOUtils.copyBytes(System.in, out, getConf(), false);

}

finally {

out.close();

}

}



void copyFromLocal(Path[] srcs, String dstf) throws IOException {

Path dstPath = new Path(dstf);



FileSystem dstFs = dstPath.getFileSystem(getConf());

if (srcs.length == 1 && srcs[0].toString().equals("-"))

copyFromStdin(dstPath, dstFs);

else

dstFs.copyFromLocalFile(false, false, srcs, dstPath);

}



public static void main(String args[])

{

try{

CopyFile fs=new CopyFile();

fs.form.setVisible(true);

}catch(Exception e) {System.out.println(e);}

}



private void init()

{



form=new JFrame();

form.setTitle("Copy File");

form.setBounds(100, 100, 450, 300);

form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

form.getContentPane().setLayout(null);





box1=new JTextField();

box1.setEditable(false);// cho phep thay doi hay khong ?--> khong.

box1.setText("Source File Path");

box1.setBounds(10, 89, 196, 20);

form.getContentPane().add(box1);

box1.setColumns(10);





box2=new JTextField();

box2.setEditable(false);// cho phep thay doi hay khong ?--> khong.

box2.setText("Destination File Path");

box2.setBounds(10, 155, 196, 20);

form.getContentPane().add(box2);

box2.setColumns(10);





sobr=new JButton("Browse");

ActionListener act1=new ActionListener() {

String str;

public void
actionPerformed(ActionEvent arg0) {



JFileChooser
fc = new JFileChooser();




fc.setFileSelectionMode(JFileChooser.FILES_ONLY);



if
(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(form)) {




box1.setText(fc.getSelectedFile().getPath());

this.str=fc.getSelectedFile().getPath();

}



}

};



sobr.addActionListener(act1);

sobr.setBounds(249, 88, 89, 23);

form.getContentPane().add(sobr);







debr=new JButton("Browse");

ActionListener act2=new ActionListener() {



public void
actionPerformed(ActionEvent arg0) {



JFileChooser
fc = new JFileChooser();




fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);



if
(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(form)) {


box2.setText(fc.getSelectedFile().getPath());



}

}

};

debr.addActionListener(act2);

debr.setBounds(249, 154, 89, 23);

form.getContentPane().add(debr);



copy =new JButton("Copy");


ActionListener act3=new ActionListener() {

public void actionPerformed(ActionEvent arg0){

String str1=box1.getText();

String str2=box2.getText();

Path[] path=new Path[1];

path[0]=new Path(str1);

try {

copyFromLocal(path, str2);

} catch (IOException ex) {


Logger.getLogger(CopyFile.class.getName()).log(Level.SEVERE, null, ex);

}





}

};

copy.addActionListener(act3);

copy.setBounds(100 , 200 , 89, 23);

form.getContentPane().add(copy);



}

}

****************************************************************************

And the output is :

****************************************************************************


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at
org.apache.hadoop.fs.FileSystem.getDefaultUri(FileSystem.java:103)

at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:95)

at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:180)

at org.apache.hadoop.fs.Path.getFileSystem(Path.java:175)

at learnsocket.CopyFile.copyFromLocal(CopyFile.java:60)

at learnsocket.CopyFile$3.actionPerformed(CopyFile.java:152)

at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6267)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

at java.awt.Component.processEvent(Component.java:6032)

at java.awt.Container.processEvent(Container.java:2041)

at java.awt.Component.dispatchEventImpl(Component.java:4630)

at java.awt.Container.dispatchEventImpl(Container.java:2099)

at java.awt.Component.dispatchEvent(Component.java:4460)

at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)

at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)

at java.awt.Container.dispatchEventImpl(Container.java:2085)

at java.awt.Window.dispatchEventImpl(Window.java:2478)

at java.awt.Component.dispatchEvent(Component.java:4460)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)

at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



********************************************************************************


Please tell me what my problem is and how to fix it.



Thanks so much ^^ !



--
Nguyễn Bá Trung
SHSV : 20083535
SĐT : 01216259222
Email : trungnb3535@gmail.com

Search Discussions

  • Robert Evans at May 16, 2011 at 5:04 pm
    What version of hadoop are you using?


    On 5/14/11 9:37 AM, "Lạc Trung" wrote:

    Hello everybody !



    This exception was thrown when I tried to copy a file from local file to
    HDFS.

    This is my program :

    ***************************************************************

    import api.InterfaceAudience;

    import java.awt.event.ActionListener;

    import java.io.IOException;

    import java.awt.event.ActionEvent;

    import java.util.logging.Level;

    import java.util.logging.Logger;

    import javax.swing.*;

    import org.apache.hadoop.conf.Configured;

    import org.apache.hadoop.io.IOUtils;

    import org.apache.hadoop.fs.FileSystem;

    import org.apache.hadoop.fs.Path;

    import org.apache.hadoop.fs.FSDataOutputStream;



    @InterfaceAudience.Private

    public class CopyFile extends Configured {



    private JFrame form;

    private JTextField box1;

    private JTextField box2;

    private JButton sobr;

    private JButton debr;

    private JButton copy;

    protected FileSystem fs;





    public CopyFile() {

    init();

    }



    private void copyFromStdin(Path dst, FileSystem dstFs) throws IOException
    {

    if (dstFs.isDirectory(dst)) {

    throw new IOException("When source is stdin, destination must be a
    file.");

    }

    if (dstFs.exists(dst)) {

    throw new IOException("Target " + dst.toString() + " already
    exists.");

    }

    FSDataOutputStream out = dstFs.create(dst);

    try {

    IOUtils.copyBytes(System.in, out, getConf(), false);

    }

    finally {

    out.close();

    }

    }



    void copyFromLocal(Path[] srcs, String dstf) throws IOException {

    Path dstPath = new Path(dstf);



    FileSystem dstFs = dstPath.getFileSystem(getConf());

    if (srcs.length == 1 && srcs[0].toString().equals("-"))

    copyFromStdin(dstPath, dstFs);

    else

    dstFs.copyFromLocalFile(false, false, srcs, dstPath);

    }



    public static void main(String args[])

    {

    try{

    CopyFile fs=new CopyFile();

    fs.form.setVisible(true);

    }catch(Exception e) {System.out.println(e);}

    }



    private void init()

    {



    form=new JFrame();

    form.setTitle("Copy File");

    form.setBounds(100, 100, 450, 300);

    form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    form.getContentPane().setLayout(null);





    box1=new JTextField();

    box1.setEditable(false);// cho phep thay doi hay khong ?--> khong.

    box1.setText("Source File Path");

    box1.setBounds(10, 89, 196, 20);

    form.getContentPane().add(box1);

    box1.setColumns(10);





    box2=new JTextField();

    box2.setEditable(false);// cho phep thay doi hay khong ?--> khong.

    box2.setText("Destination File Path");

    box2.setBounds(10, 155, 196, 20);

    form.getContentPane().add(box2);

    box2.setColumns(10);





    sobr=new JButton("Browse");

    ActionListener act1=new ActionListener() {

    String str;

    public void
    actionPerformed(ActionEvent arg0) {



    JFileChooser
    fc = new JFileChooser();




    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);



    if
    (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(form)) {




    box1.setText(fc.getSelectedFile().getPath());

    this.str=fc.getSelectedFile().getPath();

    }



    }

    };



    sobr.addActionListener(act1);

    sobr.setBounds(249, 88, 89, 23);

    form.getContentPane().add(sobr);







    debr=new JButton("Browse");

    ActionListener act2=new ActionListener() {



    public void
    actionPerformed(ActionEvent arg0) {



    JFileChooser
    fc = new JFileChooser();




    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);



    if
    (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(form)) {


    box2.setText(fc.getSelectedFile().getPath());



    }

    }

    };

    debr.addActionListener(act2);

    debr.setBounds(249, 154, 89, 23);

    form.getContentPane().add(debr);



    copy =new JButton("Copy");


    ActionListener act3=new ActionListener() {

    public void actionPerformed(ActionEvent arg0){

    String str1=box1.getText();

    String str2=box2.getText();

    Path[] path=new Path[1];

    path[0]=new Path(str1);

    try {

    copyFromLocal(path, str2);

    } catch (IOException ex) {


    Logger.getLogger(CopyFile.class.getName()).log(Level.SEVERE, null, ex);

    }





    }

    };

    copy.addActionListener(act3);

    copy.setBounds(100 , 200 , 89, 23);

    form.getContentPane().add(copy);



    }

    }

    ****************************************************************************

    And the output is :

    ****************************************************************************


    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    at
    org.apache.hadoop.fs.FileSystem.getDefaultUri(FileSystem.java:103)

    at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:95)

    at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:180)

    at org.apache.hadoop.fs.Path.getFileSystem(Path.java:175)

    at learnsocket.CopyFile.copyFromLocal(CopyFile.java:60)

    at learnsocket.CopyFile$3.actionPerformed(CopyFile.java:152)

    at
    javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

    at
    javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

    at
    javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

    at
    javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

    at
    javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

    at java.awt.Component.processMouseEvent(Component.java:6267)

    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

    at java.awt.Component.processEvent(Component.java:6032)

    at java.awt.Container.processEvent(Container.java:2041)

    at java.awt.Component.dispatchEventImpl(Component.java:4630)

    at java.awt.Container.dispatchEventImpl(Container.java:2099)

    at java.awt.Component.dispatchEvent(Component.java:4460)

    at
    java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)

    at
    java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)

    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)

    at java.awt.Container.dispatchEventImpl(Container.java:2085)

    at java.awt.Window.dispatchEventImpl(Window.java:2478)

    at java.awt.Component.dispatchEvent(Component.java:4460)

    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

    at
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

    at
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

    at
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)

    at
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

    at
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



    ********************************************************************************


    Please tell me what my problem is and how to fix it.



    Thanks so much ^^ !



    --
    Nguyễn Bá Trung
    SHSV : 20083535
    SĐT : 01216259222
    Email : trungnb3535@gmail.com
  • Lạc Trung at May 16, 2011 at 8:12 pm
    I'm using Hadoop-0.21.
    ---
    hut.edu.vn
  • Steve Loughran at May 17, 2011 at 9:45 am

    On 16/05/11 21:12, Lạc Trung wrote:
    I'm using Hadoop-0.21.
    ---
    hut.edu.vn
    At the top, it's your code, so you get to fix it. The good thing about
    open source is you can go all the way in.

    This is what I would do in the same situation
    -Grab the 0.21 source JAR
    -add it your IDE
    -have a look at what line it's NPEing on
    -work out what variable's being null would trigger that. Usually its
    whatever object has just had a method called on it.
    -work out why that's null (usually some config/startup thing was missing).
    -If its a repeatable problem, try adding some checks in advance, dump
    state to the console, etc.
    -fix what is probably a setup problem

    If it's some other problem (race condition etc), life is harder

    -steve
  • Harsh J at May 17, 2011 at 10:41 am
    Hey Lạc Trung,

    I do not see a configuration instance used in your code; but you're
    using the Configured class. Do you instantiate CopyFiles using
    Hadoop's ReflectionUtils utility class? Unless that's done, the
    getConf() would be returning a null causing the issue probably.
    On Sat, May 14, 2011 at 8:07 PM, Lạc Trung wrote:
    Hello everybody !



    This exception was thrown when I tried to copy a file from local file to
    HDFS.
    --
    Harsh J
  • Lạc Trung at May 18, 2011 at 3:33 pm
    @Steve Loughran : That's the way I repaired my program.
    @Robert Evans
    @Harsh J : Thanks for your reply.I'm so happy.

    Thanks so much.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcommon-user @
categorieshadoop
postedMay 14, '11 at 11:58p
activeMay 18, '11 at 3:33p
posts6
users4
websitehadoop.apache.org...
irc#hadoop

People

Translate

site design / logo © 2022 Grokbase