I have an application which throws this error (happens only with xlsx-files):
java.lang.NullPointerException
at java.io.File.<init>(File.java:222)
at de.mpicbg.tds.core.ExcelLayout.openWorkbook(ExcelLayout.java:75)
The method 'openWorkbook' looks like this:
private void openWorkbook() throws IOException {
File excelFile = new File(fileName);
timestamp = excelFile.lastModified();
// open excel file
if (fileName.endsWith(".xlsx")) {
InputStream excelStream = new BufferedInputStream(new FileInputStream(excelFile));
this.workbook = new XSSFWorkbook((excelStream));
} else {
this.workbook = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(excelFile)));
}
}
If I execute everything in debug mode, everything goes smoothly and the error message does not appear. I don't have any explanation for this behaviour nor any idea how to fix it.
Can anybody help?
The error message says your fileName is null
If you cannot reproduce this when debugging you can add a log message at the start of you method.
System.out.println("The fileName is `" + fileName+"`");
Instead of using a field which may or may not be set, I suggest you use a parameter.
private void openWorkbook(String fileName) throws IOException {
assert fileName != null;
Related
When I call my batch program I get a null pointer exception on this line:
String fileName = ((Map<String, MccFtpFile>) ec.get(AbstractSetupTasklet.BATCH_FTP_FILES)).get("scacFile").getLocalFile();
Here is the entire class
// Writer
#Bean(name = "clf010Writer")
#StepScope
public FlatFileItemWriter<Clf010Item> clfWriter(#Value("#{jobExecutionContext}") Map<Object, Object> ec, //
#Qualifier("clf010LineAggregator") FormatterLineAggregator<Clf010Item> lineAgg) throws IOException {
#SuppressWarnings("unchecked")
String fileName = ((Map<String, MccFtpFile>) ec.get(AbstractSetupTasklet.BATCH_FTP_FILES)).get("scacFile").getLocalFile();
//Ensure the file can exist.
PrintWriter fos = getIoHarness().getFileOutputStream(fileName);
fos.close();
FlatFileItemWriter<Clf010Item> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource(fileName));
writer.setLineAggregator(lineAgg);
return writer;
}
I can't seem to figure out the problem. My guess is it's looking for a file it can't find? If anyone has some insight or information I would appreciate it a lot.
I didn't realize, but in my setup tasklet I needed to declare a new ftp file :) thanks for the help
Error in code:
If that train is removed then also show compiler error as below. I tried a lot to remove that and classify sentence as positive or negative.
An InputStreamFactory in not the same as an InputStream. Here is a simple bit of code that will create an InputStreamFactory for you. (You might say it is an InputStreamFactoryFactory :-) )
public static InputStreamFactory getInputStreamFactory(final File file) throws IOException{
return new InputStreamFactory() {
#Override
public InputStream createInputStream() throws IOException {
return new FileInputStream(file);
}
};
}
see javadocs: https://opennlp.apache.org/documentation/1.7.2/apidocs/opennlp-tools/opennlp/tools/util/InputStreamFactory.html
public class TestScenario1 {
#Test (dataProvider = "test")
public void execute(String TestCol1,String TestCol2, String TestCol3,String TestCol4) throws Exception {
homePage hp = new homePage();
hp.perform(TestCol1, TestCol2, TestCol3, TestCol4);
}
#DataProvider(name= "test")
public Object[][] testcase(ITestContext context) {
String filepath = executionBase.CONFIG.getProperty("filepath");
// String filepath = "D://workspace//Project//data//testData.xlsx";
String sheetname = "Suite";
return executionBase.getTestData(filepath, sheetname);
}
}
If I execute above code it gives me following error:
SKIPPED: execute
java.lang.RuntimeException: java.lang.NullPointerException
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:162)
at org.testng.internal.Parameters.handleParameters(Parameters.java:430)
But if I comment this line
String filepath =executionBase.CONFIG.getProperty("filepath");
and execute following instead,
String filepath = "D://workspace//Project//data//testData.xlsx";
it works fine
executionBase.class referred constructor code looks like,
public executionBase() throws IOException, FileNotFoundException {
CONFIG = new Properties();
FileInputStream ip = new FileInputStream(System.getProperty("user.dir")+ "//config//config.properties");
CONFIG.load(ip);
}
config.properties file entry goes like this,
filepath=D:\workspace\Project\data\testData.xlsx
Function of executionBase.class works fine for other path variables provided in config.properties, but not sure why I am getting Null value for same under
#DataProvider(name= "test") annotation
Properties.load() will escape back slashes, Try using double backslashes in the config file, or changing to forward slash:
filepath=D:/workspace/Project/data/testData.xlsx
You can also try:
String content = IOUtils.toString(ip, Charset.defaultCharset());
content = content.replaceAll("\\","\\\\");
CONFIG.load(content);
Edit: From what I can see, seems like your executionBase isn't initialized when you run your TestScenario class. Check how is executionBase() is called, and verify its called before calling it in DataProvider.
I faced the exact same problem, Open you test data sheet and press "CTRL+END", if its not the cell(lastrow,lastcolumn), then manually right click on that row or column and DELETE it. Save the file and run it, worked for me !
I've developed a HTTP communication object for the purpose pof downloading files via a GET request.
This works just fine when downloading a text file. However, downloading a compressed file such as zip, gz or tar.gz appears to download the file but the file is not valid.
In the case zip I get a meesage saying it tried to move the pointer before the beginnning of the file.
In the case of .tar.gz the message is Data error in file.tar. File is broken.
In all cases the download links I use do allow a complete and correct download from the URL. Yet, the Java code based download brings the file down but it is not valid.
The code is as follows:
public class HTTPCommunicationGet {
private URIBuilder sendData;
private URI target;
private HttpGet getConnection;
public HTTPCommunicationGet(String url, TreeMap<String, String> components) {
super(url, components);
}
public HTTPCommunicationGet(String url, String queryString) {
super(url, queryString);
}
protected void defineSendData() throws URISyntaxException, IOException {
this.sendData = new URIBuilder(new URI(this.getUrl()));
if (this.getComponents() != null && this.getComponents().size() > 0) {
for (Map.Entry<String, String> component : this.getComponents().entrySet()) {
this.sendData.setParameter(component.getKey(), component.getValue());
}
}
}
protected void retrieveRemoteData() throws IOException, MalformedURLException, URISyntaxException, DataMapHTTPGetException {
this.target = this.sendData.build();
this.getConnection = new HttpGet(target);
HttpResponse response = client.execute(this.getConnection);
if (response.getStatusLine().toString().toUpperCase().contains("200 OK")) {
this.setResponse(response.getStatusLine().toString(), "Data Retrieved");
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
this.remoteData.append(line);
}
} else {
String message = String.format("%s: Provider connection exception; response returned was not 200 OK", this.target.toASCIIString());
this.setResponse(response.getStatusLine().toString(), message);
DataMapHTTPGetException ex = new DataMapHTTPGetException(target.toString(), message);
throw ex;
}
}
public void downloadFiles(String localFile) throws DataMapConnectionException, FileNotFoundException, IOException, URISyntaxException {
// check that we have remoteData set
this.defineSendData();
this.retrieveRemoteData(); // everything is bubbled up to the controller class that is calling this.
File localMetaFile = new File(localFile);
switch (this.archiveMetaFile(localMetaFile)) {
case -1:
IOException ex = new IOException(String.format("The file %s could not be moved", localFile));
throw ex;
//break;
case 0:
infoLog.info(String.format("%s: this file did not already exist", localFile));
break;
case 1:
infoLog.info(String.format("%s: this file was found and successfully archived to the processed directory", localFile));
break;
}
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(localFile));
fileWriter.write(this.remoteData.toString());
fileWriter.close();
}
}
As you can see this is called via downloadFiles after the object has been initialised. I've cut out the code that is not needed for this example such as the archiveMetaFile method.
Any pointers on why this is not working for compressed files is much appreciated.
Cheers
Nathan
The problem is likely that you are using a BufferedReader instead of an InputStream. Readers are used for text data and impose a character encoding whereas InputStreams can handle raw binary data.
Try switching to a BufferedInputStream instead. The use of any Reader class will corrupt binary data.
I'm new to Java and I'm porting one of my C++ libraries to Java as a learning experiment. This is not homework (as should be obvious from looking at my code). I have a few questions concerning the following code of my constructor for an ESRI shape file reader.
import java.io.*;
/**
*
* #author Bill
*/
public class ShapeFileReader {
public FileInputStream inStream;
/*
* #param fileName File name string. Must not be null or zero length.
* #throws Exception if file specified by fileName fails to open
*/
public ShapeFileReader(String fileName) throws IOException {
if(fileName == null)
throw new NullPointerException("fileName was null");
if(fileName.length() == 0)
throw new IllegalArgumentException("fileName string length was zero");
File fi = new File(fileName);
if(fi.exists() == false)
throw new IOException("File does not exist: " + fileName);
// Catch-or-specify (this method already throws IOException)
inStream = new FileInputStream(fileName);
}
}
During parameter validation and existence should I be throwing the exceptions as shown? The validation throws unchecked exceptions, and the existence throws checked exceptions. I assume that FileInputStream constructor will also throw an IOException, but I specified that in the method throws clause.
I was considering refactoring the opening of the file to a seperate function, but I figured it would be more useful and simple to do this in the constructor, and also learns me how to control errors here. Besides, any instance of this object will not have a closed/open state. All of these objects are reserved strictly for READING a file only, so they are created on a as-needed basis per file. I will provide a close() method seperately.
Also, from an extensibility point of view, would it be difficult to adapt to reading a file over a network using the FileInputStream with the current constructor? Or should I use a different class and multiple constructors?
Thanks for any and all input.
I wouldn't bother with the exceptions, FileInputStream will throw an exception for you, you're not adding anything other than clutter to your code.
For it to work with the network rather than just a file you'd modify thus:
public class ShapeFileReader {
private final InputStream inStream;
public ShapeFileReader(InputStream inStream) {
this.inStream = inStream;
}
/*
* #param fileName File name string. Must not be null or zero length.
* #throws Exception if file specified by fileName fails to open
*/
public ShapeFileReader(String fileName) throws IOException {
this(new FileInputStream(fileName));
}
Since this has been accepted as the answer I'm editing it as Roland (in the comments) is quite correct and this isn't how I'd have approached the problem.
public class ShapeReader {
public static Shape readShape(InputStream inStream) {
... do the work
}
/*
* #param fileName File name string. Must not be null or zero length.
* #throws Exception if file specified by fileName fails to open
*/
public static Shape readShape(String fileName) throws IOException {
FileInputStream fis = new FileInputStream(fileName);
try {
return readShape(fis);
} finally {
fis.close();
}
}
}