Actually, he cannot find java.io.
It recognizes import java.io.*;
import java.io.IOException;
but when I try InputStream = this.getClass().getResourceAsStream("users.xml");, i get the following error:
C:\Users\cpantaziu\Documents\NetBeansProjects\url\src\main\java\com\mkyong\common\controller\FailRegisterController.java:[120,12] cannot find symbol
symbol : variable InputStream.
Tools -> Java Platforms ses my jdk 1.6.
Why am I getting this?
you have to assign a reference variable:
InputStream ref= this.getClass().getResourceAsStream("users.xml");
You must name a variable:
InputStream is = this.getClas()...
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
/**
*
* #author mbarb
*/
public class LoadSave {
public static BufferedImage GetPlayerAtlas(){
BufferedImage img = null;
InputStream is =
LoadSave.class.getResourceAsStream("Res/player2.png");
try {
img = ImageIO.read(is);
} catch (IOException e) {
e.printStackTrace();
}//try catch .close()
return img;
}
}
I'm have the same issue here in the code above.
i just get this:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358)
at Utilities.LoadSave.GetPlayerAtlas(LoadSave.java:19)
at Entities.Player.loadAnimations(Player.java:102)
at Entities.Player.<init>(Player.java:29)
at com.mycompany.platformgame.Game.initClasses(Game.java:32)
at com.mycompany.platformgame.Game.<init>(Game.java:21)
at com.mycompany.platformgame.PlatformGame.main(PlatformGame.java:7)
Command execution failed.
Related
I am trying to convert a docx file into pdf file using POI. Getting following error.
Using poi-3.17 ,
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class WordToPDF {
public static void main(String[] args) {
WordToPDF cwoWord = new WordToPDF();
System.out.println("Start");
cwoWord.ConvertToPDF("D:\\2067536.docx", "D:\\2067536.pdf");
}
public void ConvertToPDF(String docPath, String pdfPath) {
try {
InputStream doc = new FileInputStream(new File(docPath));
XWPFDocument document = new XWPFDocument(doc);
document.createStyles();
PdfOptions options = PdfOptions.create();
OutputStream out = new FileOutputStream(new File(pdfPath));
PdfConverter.getInstance().convert(document, out, options);
System.out.println("Done");
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage());
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
Here is the Error happening
Exception in thread "main" org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38)
at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
at WordToPDF.ConvertToPDF(WordToPDF.java:27)
at WordToPDF.main(WordToPDF.java:17)
Caused by: java.lang.NullPointerException
at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.visitHeader(PdfMapper.java:178)
at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.visitHeader(PdfMapper.java:111)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitHeaderRef(XWPFDocumentVisitor.java:1142)
at org.apache.poi.xwpf.converter.core.MasterPageManager.visitHeadersFooters(MasterPageManager.java:213)
at org.apache.poi.xwpf.converter.core.MasterPageManager.addSection(MasterPageManager.java:180)
at org.apache.poi.xwpf.converter.core.MasterPageManager.compute(MasterPageManager.java:127)
at org.apache.poi.xwpf.converter.core.MasterPageManager.initialize(MasterPageManager.java:90)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.visitBodyElements(XWPFDocumentVisitor.java:232)
at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:199)
at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:56)
... 4 more
As this is a null pointer error I am unable to understand what exactly the issue might be, any help is appreciated. Thank you.
Libre Office Saved my life, Simple one liner command for docx to pdf conversion works like a charm.
Detailed answer here
Command `libreoffice --headless --convert-to pdf test.docx --outdir /pdf` is not working
ImageIO.read() just seems to be stuck in an infinite loop.
My code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Texture {
BufferedImage _img;
public Texture(String path) {
try {
_img = ImageIO.read(new File(path));
} catch (final IOException e) {
e.printStackTrace();
}
}
}
Other class:
private Texture _tex;
_tex = new Texture("/res/img.png");
I have tried loading the image this URL and File, none works.
I am using eclipse, on a mac, and I am using LWJGL 3 if that has anything to do with anything.
Hope you can help me! :-)
Instead of using a File, try using an input stream like this:
InputStream stream = Texture.class.getResourceAsStream(path);
_img = ImageIO.read(stream);
I'm actually experiencing a similar issue, but it's related to the creation of BufferedImages.
I'm trying to learn how to program frames in Java and I ran into a problem. When I create a jar file of my code I come up with a blank screen. My previous jar files don't have the same problem. When I ran the jar file through command prompt I got the following error message:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at getResources.getImg(getResources.java:31)
at MyCanvas.paint(gameLoop.java:99)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
…
Here is the code for the main class and MyCanvas class:
class MyCanvas extends JComponent{
public void paint(Graphics g){
getResources get = new getResources();
BufferedImage titleImg = get.getImg("Title");
BufferedImage testImg = get.getImage("");
g.drawImage(titleImg, 0, 0, null);
g.drawImage(testImg, 0 , 0, null);
}
}
Here is the code for my "getResources" class:
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.Font;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/**
* Write a description of class getResources here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class getResources{
public void getResources(){
}
public BufferedImage getImg(String path){
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResource("/Sprites/" + path + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public BufferedImage getImage(String path){
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResource("/Sprites/test.png"));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public AudioStream getSeven(){
InputStream in;
AudioStream as = null;
try{
in = this.getClass().getResourceAsStream("/Music/seven.wav");
as = new AudioStream (in);
}catch(Exception e){
e.printStackTrace();
}
return as;
}
}
I am pretty sure that the error is in the "getImg" method and the way it retrieves images from the source folder. I removed all the lines of codes that had references to that method and the jar file ran just fine. However, the "getImage" class runs just fine in the jar file. Maybe something about passing a string to get the path of the image creates the error? I'd like to know if anyone knows what is causing the error and if I should even be concerned by not being able to run jar files, I do like using them.
I have tested my program on windows platform, works perfectly. But when I try to run it on Raspbian, Unix, it doesn't go further after iplcvLoadimage.
What could be the problem?
Here is my code
mport com.googlecode.javacv.cpp.opencv_core.CvPoint;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.cvSize;
import static com.googlecode.javacv.cpp.opencv_core.cvZero;
import static com.googlecode.javacv.cpp.opencv_core.cvMinMaxLoc;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_32F;
import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvMatchTemplate;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_TM_SQDIFF;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import org.opencv.core.Core;
public class MatchTemplateTest {
public static HashMap<String,Double> getPatternMatch(String img)
{
ArrayList<String>names=new ArrayList<>();
HashMap<String,Double> MatchMap=new HashMap<>();
File []f=null;
try
{
String path=new File(new File(".").getCanonicalPath()+"/Output"+"/Logo").getAbsolutePath();
f=new File(path).listFiles();
} catch (Exception e) {
}
IplImage src=null;
try {
// this is where the problem is
src= cvLoadImage(img,0);
// can not process further
} catch (Exception e) {
System.out.println("ERROR "+e);
}
If the problem is with reading the image, then there are two possiblities
The image does not exist, or the path to the image is wrong. In *nix the path is separated with a slash '/', e.g. /home/asharma/data/myimage.pgm, but in windows it is separated with a backslash, e.g. C:\User\Data\My Image.pgm
The image decoder for the image does not exist in the version of the library. For example, if the image is in PNG format, but opencv was not compiled with PNG, then you cannot read the image.
what is the output of System.out.println("ERROR "+e);?
Here is my code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import model.Map;
public class MyView {
private BufferedImage img = null;
private static MyPanel panel;
//init image
try{
img = ImageIO.read(new File("/src/minepic/start.png"));
} catch (IOException e){
System.out.println(e.getMessage());
}
}
I want to load an PNG image from the src directory but I don't know why it doesn't work, anyone can help me?
Error in command "try catch" and NetBeans say "unreported exception IOexception; must be caught or declared to be thrown"
One more, even i declared img as a BufferedImage before, but in command "try catch" img just like didn't declared because in NetBeans it doesn't become green, still black.
There are few issues with the code:
It is incomplete. The braces are not matched.
you have written code outside main method (possible but not recommended)
To read the image from src folder (which is a part of your class path) use the below snippet:
Inputstream is = MyView.class.getResourceAsStream("minepic/start.png");
if(is==null){
is = MyView.class.getClassLoader().getResourceAsStream("minepic/start.png");
}
img = ImageIO.read(is);