When I call Velocity.init() I get the following exception. I have included all dependencies, I have tried setting properties and using property files but all to no avail. any help?
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:97)
at org.apache.commons.collections.ExtendedProperties.load(ExtendedProperties.java:543)
at org.apache.commons.collections.ExtendedProperties.load(ExtendedProperties.java:519)
at org.apache.velocity.runtime.RuntimeInstance.setDefaultProperties(RuntimeInstance.java:416)
at org.apache.velocity.runtime.RuntimeInstance.initializeProperties(RuntimeInstance.java:628)
at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:261)
at org.apache.velocity.runtime.RuntimeSingleton.init(RuntimeSingleton.java:112)
at org.apache.velocity.app.Velocity.init(Velocity.java:74)
here is the calling code
import com.syntatik.roborm.RobormEntity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.Properties;
public class Generator{
protected VelocityContext context;
public Generator(){
Velocity.init();
this.context = new VelocityContext();
}
}
I have looked at the Velocity source code and can see that it's finding the default properties file on your classpath and then it's having trouble in Reader.java. With an IDE you should be able to get the source to Reader.java to find out why it's getting the NPE. Unfortunately at the moment I can't look at Reader to give you more information.
If you can't determine the cause of the NPE, I suggest you delete the default property file from your classpath and set the properties in code.
Have a look at this question for more information
Related
I'm making a Java Spring project in IntelliJ as an exercise and I don't understand a file configuration error I'm having.
Please look at my project directory layout as described in this screenshot:
The three most important files here are the Main & GameConfig classes, and the game.properties file. Or, to use their full package directory names, "console/src/main/java/academy.learnprogramming.console.Main", "core/src/main/java/academy.learnprogramming.config.GameConfig" and core/src.resources.config.game.properties
When I run project, the Main class tries to run the following code:
package academy.learnprogramming.console;
import academy.learnprogramming.MessageGenerator;
import academy.learnprogramming.NumberGenerator;
import academy.learnprogramming.config.GameConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
private static final Logger log = LoggerFactory.getLogger(com.sun.tools.javac.Main.class);
public static void main(String[] args) {
log.info("Guess the number");
//== create the context first ==
ConfigurableApplicationContext context
= new AnnotationConfigApplicationContext(GameConfig.class);
// rest of code, which we never get to because of an error with the above line
When I try to create ConfigurableApplicationContext, I am trying to call the GameConfig class from the Core package. But I get this runtime error instead:
Exception in thread "main"
org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to parse configuration class
[academy.learnprogramming.config.GameConfig]; nested exception is
java.io.FileNotFoundException: class path resource
[config/game.properties] cannot be opened because it does not exist
I do not understand why it thinks GameConfig does not exist, or how to fix it. No solutions I have found seem to have the precise answer for this. I have gotten a configuration comparison from someone whose code is identical to mine, but neither of us understand why their version works and mine does not.
For comparison's sake, the GameConfig class has the following code:
package academy.learnprogramming.config;
import academy.learnprogramming.GuessCount;
import academy.learnprogramming.MaxNumber;
import academy.learnprogramming.MinNumber;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
#Configuration
#ComponentScan(basePackages = "academy.learnprogramming")
#PropertySource("classpath:config/game.properties")
public class GameConfig {
//rest of GameConfig code here...
While game.properties is as simple as this:
game.maxNumber = 100
game.guessCount = 10
game.minNumber = 0
I can't find a question similar to this, nor do other websites point out what's going on. Does anyone have any idea why this error might be happening? Any advice would be invaluable
I cam across the following Logger definition in my Java code and noticed that the following LoggerFactory uses the .create() method. I tried to locate good documentation to explain the difference between LoggerFactory.getLogger(getClass()) and LoggerFactory.create(getClass()) but seem to have difficulties figuring out what exactly they do differently.
The package supporting the imports is called: javautils-lib-3.0.jar.
import utils.log.ILogger;
import utils.log.Log;
import utils.log.LogLevel;
import utils.log.Logger;
import utils.log.LoggerFactory;
public static final ILogger LOG = LoggerFactory.create(LoggedExperiment.class);
Was wondering whether anyone would be able to point me in the right direction?
Have a great day.
M
So I'm working on reading in a ".txt" file to use it to implement Dijkstra's algorithm, but every time I try to compile it gives me a "FileReader is already defined in this compilation unit" error while highlighting where I imported it in the beginning. If I take this out, however, it throws a constructor error when I'm trying to read in the file that it's of the wrong type. What am I missing here??
Here is my code:
import java.io.BufferedReader;
import java.io.File;
//import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileReader
{
public ArrayList main1()
{
System.out.println("got here");
try
{
BufferedReader in = new BufferedReader(new FileReader(new File("input1.txt")));
I can provide more if needed, but this is where all of the errors crop up.
Your class is named the same as FileReader in the java.io package (you have commented out above). Rename your class to something else like TextFileReader or InputFileReader or use the fully qualified class name for java.io.FileReader.
Just rename your class "FileReader" to different toxicity, in order not to be confused.
I'm trying to setup an application that runs on OSGi internally and have tried using the tutorial here, but I get the error "The method getBundleContext() is undefined for the type Framework" all the time. As far as I can tell, I'm using the right library, but it's not specified in the mentioned article, so I'm not 100% sure. I've also tried the examples on Apache's website, here, which results in the same issue. Code below:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
public class Main {
public static void main(String[] args) throws BundleException {
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
Framework framework = frameworkFactory.newFramework(config);
framework.start();
// Throws error that it cannot find method getBundleContext()
BundleContext context = framework.getBundleContext();
List<Bundle> installedBundles = new LinkedList<Bundle>();
installedBundles.add(context.installBundle("file:org.apache.felix.shell-1.4.2.jar"));
installedBundles.add(context.installBundle("file:org.apache.felix.shell.tui-1.4.1.jar"));
for (Bundle bundle : installedBundles) {
bundle.start();
}
}
}
The only thing that makes sense is that either I'm using the wrong libraries, or the libraries have changed and the method I'm attempting to call has since been deprecated out in the last 4 years. Anyone know how I can fix this?
I doubt it makes much of a difference, but in case it does, I'm using Bndtools for Eclipse to create this project.
Found the issue. Apparently, the import of osgi.core that was in the Bndtools' project build path was out of date, preventing the code from accessing the correct version of the framework libraries. Updating that fixed the issue.
Additional side-note; Since I'm using Bndtools, I was adding this to the project build path via the bnd.bnd file's build tab. This, however, was not grabbing the correct version of osgi.core, so I had to go under source and add the version=latest in order to force it to get the latest version available, so the line now appears as: osgi.core;version=latest where it was previously just osgi.core under the -buildpath: section.
I know this is a common question asked but I've been searching and I've included the class into eclipse through the buildpath. I start to write the import statement and it autocompletes options for me so I know it's finding the class.
My problem is how come it's giving this error when I'm reading the docs and it says the constructor method is MimeUtil2() ?
http://www.jarvana.com/jarvana/view/eu/medsea/mimeutil/mime-util/2.1/mime-util-2.1-javadoc.jar!/eu/medsea/mimeutil/MimeUtil2.html#MimeUtil2()
package com.jab.app;
import java.io.File;
import eu.medsea.mimeutil.*;
public class CheckFileType {
private void GetMimeType(File filename){
MimeUtil2 test = new MimeUtil2(); //Produces the error saying java type cannot be resolved
}
I think you need to import
import eu.medsea.mimeutil.*;
According to the documentation, the type is eu.medsea.mimeutil.MimeUtil2
I ended up finding out that I was using the test-source.jar not the main jar file itself. The sourceforge page made the default as the source file instead of the main jar file.
It was buried inside of the files page.