-2

Having a problem with this code have been at it for quite a bit and have looked and played with it on multiple different occasions and angels and can't seem to get it going its not accepting the File underlined in red (Code Below) This belongs to the Dropbox Api the method in which Im trying to create is to upload a clients file to the dropBox... Im using a JFileChooser to open the file then putting it into a selected File and than a fileInputStream and this is where the problem is arising no matter which way I try and work around it, ints not budging. I have never worked with this API before but have looked at a lot of different exp. code and stilll can't seem to get it even after looking at their code?

package daword;

 import java.util.ArrayList;
import java.awt.Desktop;
import java.io.File;
import com.dropbox.core.DbxAccountInfo;
import com.dropbox.core.*;
///import com.dropbox.core.DbxEntry.File;
import com.dropbox.core.json.JsonReader;
import com.dropbox.core.json.JsonReader.FileLoadException;
import java.awt.BorderLayout;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.stage.FileChooser;
import javax.imageio.ImageIO;
import javax.naming.Context;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.DefaultEditorKit.CopyAction;
import javax.swing.text.DefaultEditorKit.CutAction;


public class dropBox extends JFrame implements ActionListener{
  File inputFile;   
private boolean logedIn = false;
JFileChooser fc;
 String dirName;
final String APP_KEY = "00000";
final String APP_SECRET = "00000";
private JButton connect, upload;
ImageIcon im1;
DbxClient client;
JLabel jlab;
JPanel jpan;
ImageIcon login;
BorderLayout borderlayout;
JTextField field = new JTextField(15);
JPopupMenu menu = new JPopupMenu("Menu");

//boolean  isAuthTokenReady = false;
InetSocketAddress add;
//boolean connection = false;
public JButton jb;



public dropBox(){
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(600,600);
setLocationRelativeTo(null);
setResizable(false);
setLookAndFeel();

JOptionPane.showMessageDialog(null,"1.Please Click Login\n2.Click Options\n3.Use How You Please!",
"Instructions", JOptionPane.WARNING_MESSAGE);
internet();

borderlayout = new BorderLayout();
getClipboard();
im1 = new ImageIcon(getClass().getResource("dropBox.gif"));
jlab= new JLabel(im1);
jlab.setLayout(null);
add(jlab);
login = new ImageIcon(getClass().getResource("login.gif"));
connect = new JButton( login);

connect.setSize(180, 57);
jlab.add(connect);
connect.addActionListener(this);


upload = new JButton ("upload");
upload.setBounds(0, 0, 220, 30);
upload.addActionListener(this);
jlab.add(upload);
BorderLayout bord = new BorderLayout();



setVisible(true);
}



 public void internet (){
Socket sock = new Socket();
  add = new InetSocketAddress("www.google.com", 80);
   try{
       wifi= true;
       sock.connect(add, 3000);
       System.out.println("Connection Present");
       ImageIcon img = new ImageIcon("/images/wifi.png");
       jb = new JButton(img);


   } catch(IOException e){

       System.out.println(" No Connection");
   }

 }





@Override
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
Object uploadS = evt.getSource();
if(source == connect){
try{
drop();
    }catch(Exception e){
JOptionPane.showMessageDialog(null,"Unable To Connect :( Please Try Again ..",
"ALERT", JOptionPane.WARNING_MESSAGE);

}

}else if(uploadS == upload){

       if (logedIn = true){
           try {
               uploadFile();
           } catch (DbxException ex) {
               Logger.getLogger(dropBox.class.getName()).log(Level.SEVERE, null, ex);
           } catch (IOException ex) {
               Logger.getLogger(dropBox.class.getName()).log(Level.SEVERE, null, ex);
           }
       }



}
}



private void setLookAndFeel() {
try{
UIManager.setLookAndFeel("UIManager.getSystemLookAndFeelClassName");
}catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
UnsupportedLookAndFeelException e){
}
}


public static String getClipboard() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return (String) t.getTransferData(DataFlavor.stringFlavor);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


// JButton Login
public void drop() throws IOException, DbxException{

 String userLocale = Locale.getDefault().toString();
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
DbxRequestConfig config = new DbxRequestConfig("Daword",
Locale.getDefault().toString());
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
// Have the user sign in and authorize your app.
String authorizeUrl = webAuth.start();
System.out.println("You Will Be Redirected To: " + authorizeUrl);
JOptionPane.showMessageDialog(null,"Go To:"+ authorizeUrl,
"" +
"", JOptionPane.WARNING_MESSAGE);
java.awt.Desktop.getDesktop().browse(java.net.URI.create(authorizeUrl));
System.out.println("Enter Your auth code in this prompt and hit enter and wait..");



String userA = JOptionPane.showInputDialog(null, "Enter your auth code here:"); //Prompt for

if (userA.isEmpty()) {
System.exit(1);
}
System.out.println(userA.trim()); //For testing purposes print the trimmed auth code to console.
// [Omitted Code] //
// This will fail if the user enters an invalid authorization code.


DbxAuthFinish authFinish = webAuth.finish(userA);
String accessToken = authFinish.accessToken;
 client = new DbxClient(config, accessToken);
DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.accessToken, appInfo.host);
System.out.println("Linked account: " + client.getAccountInfo().displayName);


JOptionPane.showMessageDialog(null, "Welcome"+
client.getAccountInfo().displayName+ authFinish.accessToken+
" And Welcome To Our Community!");

 }




public void uploadFile() throws FileNotFoundException, DbxException, IOException{

 fc = new JFileChooser();
 fc.setMultiSelectionEnabled(true);
 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

int dialog = fc.showSaveDialog(this);

 if (dialog == JFileChooser.APPROVE_OPTION) {
  inputFile = fc.getSelectedFile().getAbsoluteFile();




    try (FileInputStream inputStream = new FileInputStream(inputFile)) {

        DbxEntry.File uploadedFile = client.uploadFile("",
        DbxWriteMode.add(), inputFile.length(), inputStream);
        String sharedUrl = client.createShareableUrl("");
        System.out.println("Uploaded: " + uploadedFile.toString() + " URL " + sharedUrl);
    }      



}
}
}
TravJav92
  • 117
  • 1
  • 2
  • 10
  • 2
    You might start by saying what message goes with the red underline. (I assume it's an IDE that's giving you the red underline. Presumably it can also tell you the actual error.) – user94559 Dec 29 '15 at 06:00
  • what is the message ? – Ketu Dec 29 '15 at 06:13
  • Please post your compiler output and complete non working code (i.e. for example, what is `rootDir` etc) –  Dec 29 '15 at 06:27
  • Thanks for the replies guys, Alright so the red line states that ' no suitable contractor found for File when hovering over the File inputFile = new File(rootDir); Along with the FileInputStream. "rootDir" is a String sorry for not clarifying, The OUTPUT I have put up top right below all the code for you guys to view . I should confirm everything is working well in context of the program itself just not this particular method I have created a GUI for the user to sign in and connect his dropbox too its just this method specifically giving me a hard time.. thank you for trying to help out! – TravJav92 Dec 29 '15 at 18:36
  • Constructor** not contractor – TravJav92 Dec 29 '15 at 19:16

2 Answers2

0

Sounds like you're missing import java.io.File;

user94559
  • 59,196
  • 6
  • 103
  • 103
  • Thanks smarx that helped the red underline I can't believe I Forgot to import that..Just fighting with the JFileChooser at the moment Still not allowing Files to be took in – TravJav92 Dec 31 '15 at 22:48
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at daword.dropBox.uploadFile(dropBox.java:263) at daword.dropBox.actionPerformed(dropBox.java:167) – TravJav92 Dec 31 '15 at 23:09
  • That exception seems to be in code you haven't shared, so I don't see how anyone will be able to guess at the problem. – user94559 Dec 31 '15 at 23:26
  • complete code I am working with in the dropBox class has been submitted above main edit area , thank you. – TravJav92 Jan 01 '16 at 04:13
  • And which is line 263 and 167? Whatever line has the null reference exception, just try logging the value of the objects you're using to see which is null. – user94559 Jan 01 '16 at 07:29
0

This has been solved essentially what happened as smarx pointed out is not enough red-bulls for me to notice to import the java.io as I had a dbx file import that was conflicting.. I also didn't notice that I didn't include a code to store my access codes in so everytime I ran the program I should have signed in or should have had a key stored. In short the File path was also causing problems as I assumed the File Chooser got the code and would pass it to the input stream which it did not do, absolute path to the file location exp: user/me/desktop/cool.txt was needed. I looked over the dropbox documentation more on how the proper parameters and with the help ope this community pieced it together thanks everyone for replying.

TravJav92
  • 117
  • 1
  • 2
  • 10