Cannot find symbol - method getAbsolutePath() java [duplicate] - java

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I try to use the method "getAbsolutePath()" but I always get the same error.
Here is how I try to use it :
class OpenFrequenciesL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
final FileNameExtensionFilter filter = new FileNameExtensionFilter("Text", "txt");
fileChooser.setFileFilter(filter);
int fileChooserResult = fileChooser.showOpenDialog(MyGUI.this);
if (fileChooserResult == JFileChooser.APPROVE_OPTION) {
filename.setText(fileChooser.getSelectedFile().getName());
dir.setText(readLineByLineJava8(fileChooser.getAbsolutePath()));
} if (fileChooserResult == JFileChooser.CANCEL_OPTION) {
filename.setText("You pressed cancel");
dir.setText("");
}
}
}
I might need to import something but honestly I'm not even sure what I'm doing wrong at this point. Here's all that I imported :
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import javax.swing.text.JTextComponent;
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import java.io.File;

The JFileChooser class does not have a getAbsolutePath() method.
What is the dir variable?
It looks like you want perhaps...
fileChooser.getCurrentDirectory().getAbsolutePath()

This is because you are trying to call JFileChooser's method getAbsolutePath(), which does not exist. Instead you should replace the dir.setText(... line with:
dir.setText(readLineByLineJava8(fileChooser.getSelectedFile().getAbsolutePath()));
This is because JFileChooser does not have have a getAbsolutePath() method, so you need to get a File object for the file it has selected (returned by getSelectedFile()) and call getAbsolutePath() on that to get the filepath.
I hope this helps!

Related

Java "Syntax Error on token ";", { expected after this token" error

I am stuck on an error I keep getting, I have isolated the area the error has developed but cant figure out the cause.
package guiProject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTree;
public class mainWindow {
// imports and class definition are before this point
private Path adminList =
Paths.get("src/guiProject/AdminList.txt").toAbsolutePath();
try {
List<String> admins = Files.lines(adminList).collect(Collectors.toList());
} catch (IOException e) {
}
// rest of code
The error is occurring at the end of the line that defines adminList. Any help is appreciated.
Put your code inside a method - say pullAdminList . Moreover private access of adminList works just fine, because, again your methods - member of class, can access it:
public List<String> pullAdminList()
{
List<String> admins;
try
{
admins = Files.lines(adminList).collect(Collectors.toList());
}
catch (IOException e) {
}
return admins;
}

Where is "from" imported from camel in java?

I am trying to import csv file to xml file
i see apache has feature to do
from(in)
.to(out)
.split(body().tokenize("\n")).streaming()
.unmarshal().csv();
but i have a "cannot resolve method 'from(java.lang.String)' error
when I try to import then i cannot find any packages for camel
this one works:
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
what is the package to use "from" from org.apache.camel.???
this is my file:
import org.apache.camel.Exchange;
import org.apache.camel.dataformat.bindy.BindyAbstractDataFormat;
import org.apache.camel.dataformat.bindy.BindyAbstractFactory;
import org.apache.camel.dataformat.bindy.BindyFixedLengthFactory;
import org.apache.camel.dataformat.bindy.FormatFactory;
import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
import org.apache.camel.dataformat.bindy.util.ConverterUtils;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
public class Csvtoxml {
public static void convert(String in, String out) throws Exception {
DataFormat bindy = new BindyCsvDataFormat(Model.class);
from("myCsvFile.csv")
.to("myXmlFile.xml")
.split(body().tokenize("\n")).streaming()
.unmarshal().csv();
}
}
It is not imported from anywhere. In order to use it this way you have to inherit your class from org.apache.camel.builder.RouteBuilder

Take a screen shot and copy through FileUtils in Selenium WebDriver and java [duplicate]

This question already has answers here:
FileUtils not showing suggestion to import predefined class for Screenshot functionality in selenium WebDriver
(5 answers)
Closed 4 years ago.
When I try to get screen shots with following code it is showing error.
FileUtils.copyFile(source, new File("*./Screenshots/facebook.png"));
error message
but when i try the following code it is okay.
FileHandler.copy(source,new File("*./Screenshots/facebook.png"));
Why is that?
full code is below
package sample.code;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.testng.annotations.Test;
public class ScreenShot {
#Test
public void facebookScreenShot() throws IOException {
WebDriver driver= new ChromeDriver();
driver.get("http://www.facebook.com");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='u_0_m']")).sendKeys("screenshot");
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
FileHandler.copy(source,new File("*./Screenshots/facebook.png"));
driver.quit();
}
}
By using Robot class ,you can take screenshot.Following is the code to take screenshot.
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class CaptureScreenshot {
public static void main(String[]args) throws IOException, HeadlessException, AWTException
{
// This code will capture screenshot of current screen
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
// This will store screenshot on Specific location
ImageIO.write(image, "png", new File("C:\\Users\\Screenshot\\CurrentScreenshot.png"));
}
}

Benders.Strategy using Java and opl

I'm solving a mathematical model using Java however when i tried to call the Benders Strategy i keep receiving this error:
Exception in thread "main" java.lang.IllegalArgumentException: No enum class ilog.cplex.cppimpl.IloCplex$IntParam with value 1501
at ilog.cplex.cppimpl.IloCplex$IntParam.swigToEnum(IloCplex.java:1974)
at ilog.opl.IloCplex.setParam(IloCplex.java:5640)
Here's a part of my code in Java (i'm using CPLEX 12.8 and the library oplall.jar) :
import ilog.concert.IloException;
import ilog.concert.IloIntMap;
import ilog.concert.IloIntSet;
import ilog.concert.IloSymbolSet;
import ilog.opl.IloCplex;
import ilog.opl.IloOplDataSource;
import ilog.opl.IloOplErrorHandler;
import ilog.opl.IloOplFactory;
import ilog.opl.IloOplModel;
import ilog.opl.IloOplModelDefinition;
import ilog.opl.IloOplModelSource;
import ilog.opl.IloOplSettings;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
...
IloOplFactory.setDebugMode(false);
IloOplFactory oplF = new IloOplFactory();
IloOplModelSource source = oplF.createOplModelSource("Model.mod");
IloOplDataSource dataSource = oplF.createOplDataSource("Instance.dat");
IloOplErrorHandler handler = oplF.createOplErrorHandler();
IloOplSettings settings = oplF.createOplSettings(handler);
IloOplModelDefinition def = oplF.createOplModelDefinition(source, settings);
IloCplex cplex = new IloCplex();
IloOplModel opl = oplF.createOplModel(def, cplex);
opl.addDataSource(dataSource);
cplex.setParam(IloCplex.IntParam.Benders.Strategy, 3);
opl.generate();
cplex.solve();
cplex.end();
There's a similar question here.
In model.mod you could write:
execute { cplex.bendersstrategy=3; }

Strange Behaviour in Eclipse 3.8.1

I'm a newbie here. I have a simple problem in ONE java source file: the row System.out.pritln(...) has been treated as an erroneous expression. Here's the code snippet:
package vk.gui;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class MatrixSheet1 {
Properties p;
File file;
Document document;
PdfWriter writer;
Image logo = null;
Image EANimg = null;
float mnoz = new Double(72/25.6).floatValue();
int IMG_WIDTH= new Double(35*mnoz).intValue();
int IMG_HEIGHT=new Double(35*mnoz).intValue();
String err=p.getProperty("cell.height");
System.out.println("Arrgh!"); ///-------------->ERROR!
float cell_Height = Float.parseFloat(p.getProperty("cell.height"))*mnoz;
float cell_Width = Float.parseFloat(p.getProperty("cell.width"))*mnoz;
The reported error is
Multiple markers at this line
Syntax error on token ""Arrgh!"", delete this token
Syntax error on token(s), misplaced construct(s)
The sout and sysout shortcuts do not work neither. In other existing source files of same package everything is OK, the shortcuts work and the expression does not trigger an error.
I tried to create another source file and copy/paste the content, but I got the same error. What and where went wrong?
I need the printing just for debugging, but this is a bit annoying symptom.
Thanks in advance.
This happens because you can use System.out.println() only inside methodes. If you would do something like this, it would work:
public class MatrixSheet1 {
Properties p;
File file;
Document document;
PdfWriter writer;
Image logo = null;
Image EANimg = null;
float mnoz = new Double(72/25.6).floatValue();
int IMG_WIDTH= new Double(35*mnoz).intValue();
int IMG_HEIGHT=new Double(35*mnoz).intValue();
String err=p.getProperty("cell.height");
systemMessage("Argh!");
float cell_Height = Float.parseFloat(p.getProperty("cell.height"))*mnoz;
float cell_Width = Float.parseFloat(p.getProperty("cell.width"))*mnoz;
private void systemMessage(String message){
System.out.println(message);
}
}

Categories